AndroidスライドモニターRecyclerViewラインストリーム+左右のカット+上下移動
7873 ワード
余計なことを言わないで、直接コードを貼ります。具体的なコードは以下の通りです。
以上は小编で绍介したAndroidスライドモニターRecyclerViewライン+左右のカット+上下移动です。みなさんに助けてほしいです。もし何かご质问がありましたら、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="c.example.jreduch10.Recycler1Activity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"
></android.support.v7.widget.RecyclerView>
</RelativeLayout>
<span style="font-size:18px;">package c.example.jreduch10;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import c.example.jreduch10.entity.Video;
import jp.wasabeef.recyclerview.adapters.AlphaInAnimationAdapter;
import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
public class Recycler1Activity extends AppCompatActivity {
private RecyclerView rv;
private List<Video> list;
private Adapter adapter;
private ItemTouchHelper itemTouchHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler1);
rv=(RecyclerView)findViewById(R.id.rv);
list=new ArrayList<>();
initData();
adapter=new Adapter(list);
LinearLayoutManager llm=new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
// rv.setAdapter(adapter);
//
AlphaInAnimationAdapter alphaInAnimationAdapter=new AlphaInAnimationAdapter(adapter);
rv.setAdapter(new ScaleInAnimationAdapter(alphaInAnimationAdapter));
//
// rv.setItemAnimator(new DefaultItemAnimator());
itemTouchHelper=createItemTouchHelper();
itemTouchHelper.attachToRecyclerView(rv);
////
// RecyclerViewItemListener listener=new RecyclerViewItemListener(this, new RecyclerViewItemListener.OnItemClickListener() {
// @Override
// public void OnItemClick(View item, int adapterPosition) {
// Toast.makeText(getBaseContext(),adapterPosition+"",Toast.LENGTH_SHORT).show();
// }
// });
// rv.addOnItemTouchListener(listener);
}
public ItemTouchHelper createItemTouchHelper(){
itemTouchHelper=new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//
int dragFlags=ItemTouchHelper.UP|itemTouchHelper.DOWN;
//
int swipeFlags=ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
// makeMovementFlags() TouchHelper
return makeMovementFlags(dragFlags,swipeFlags);
}
// 。
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
adapter.move(viewHolder.getAdapterPosition(),target.getAdapterPosition());
return true;
}
// 。
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.delete(viewHolder.getAdapterPosition());
}
@Override
public boolean isItemViewSwipeEnabled() {
return super.isItemViewSwipeEnabled();
}
@Override
public boolean isLongPressDragEnabled() {
return super.isLongPressDragEnabled();
}
});
return itemTouchHelper;
}
public void initData(){
Video video=new Video(R.mipmap.zyf," ","........");
list.add(video);
video=new Video(R.mipmap.zyfzyf," ","........");
list.add(video);
video=new Video(R.mipmap.zyfzyfzyf," ","........");
list.add(video);
video=new Video(R.mipmap.zyf," ","........");
list.add(video);
video=new Video(R.mipmap.g," ","........");
list.add(video);
video=new Video(R.mipmap.ic_launcher," ","........");
list.add(video);
video=new Video(R.mipmap.zyf," ","........");
list.add(video);
video=new Video(R.mipmap.zyf," ","........");
list.add(video);
video=new Video(R.mipmap.zyfzyf," ","........");
list.add(video);
video=new Video(R.mipmap.zyfzyfzyf," ","........");
list.add(video);
video=new Video(R.mipmap.zyf," ","........");
list.add(video);
video=new Video(R.mipmap.g," ","........");
list.add(video);
video=new Video(R.mipmap.ic_launcher," ","........");
list.add(video);
video=new Video(R.mipmap.zyf," ","........");
list.add(video);
}
public class Adapter extends RecyclerView.Adapter<ViewHolder>{
private List<Video> mData;
public Adapter(List<Video> mData){
this.mData=mData;
}
//
public void delete(int position){
mData.remove(position);
notifyItemRemoved(position);
}
//
public void move(int from,int to){
Video video=mData.remove(from);
mData.add(to>from?to-1:to,video);
notifyItemMoved(from,to);
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=getLayoutInflater().inflate(R.layout.recycle1_linear_layout,parent,false);
ViewHolder vh=new ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Video video=mData.get(position);
holder.iv.setImageResource(video.getImg());
holder.title.setText(video.getTitle());
holder.content.setText(video.getContent());
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView iv;
TextView title;
TextView content;
public ViewHolder(View itemView) {
super(itemView);
iv= (ImageView) itemView.findViewById(R.id.iv);
title=(TextView)itemView.findViewById(R.id.title);
content=(TextView)itemView.findViewById(R.id.content);
//
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position=getAdapterPosition();
Log.d("9999999999","uuuuu");
Toast.makeText(getBaseContext(),title.getText()+":"+position,Toast.LENGTH_SHORT).show();
}
});
}
}
}
</span>
以上は小编で绍介したAndroidスライドモニターRecyclerViewライン+左右のカット+上下移动です。みなさんに助けてほしいです。もし何かご质问がありましたら、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。