Android Recycleview Item 点击选择事件以及选中后标记不同背景色滑动不出现错乱

释放双眼,带上耳机,听听看~!

实现recycleview使用基本流程,点击事件,长按事件,item选择后有特殊标记其余item恢复原状,滑动不出现错乱问题。伪代码如下:

Activity代码

public class VideoOnDemandActivity extends Activity {
private RecyclerView mVieoRecycleview;
private TitileBarView dateTimeView;
private SuspectInfoModel suspectInfo;
private VideoListAdapter videoListAdapter;
private ArrayList<VideoListBean> mVideoList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_on_demand);
suspectInfo = SuspectInfoStore.getInstance().suspectInfo;
initView();
}
private void initView() {
mVideoList = new ArrayList<>();
mVieoRecycleview = findViewById(R.id.video_recycleview);
mVieoRecycleview.setHasFixedSize(true);
mVieoRecycleview.setLayoutManager(new LinearLayoutManager(this));
dateTimeView = (TitileBarView) findViewById(R.id.dateTimeView);
if (suspectInfo != null) {
dateTimeView.setType("登陆用户:");
dateTimeView.setUserName(suspectInfo.getXM());
dateTimeView.setTitle("视频点播");
videoListAdapter = new VideoListAdapter(this,mVideoList,"");
mVieoRecycleview.setAdapter(videoListAdapter);
videoListAdapter.setmOnItemClickListener(new VideoListAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position, ArrayList<VideoListBean> msg) {
Toast.makeText(VideoOnDemandActivity.this, "点击了"+position, Toast.LENGTH_SHORT).show();
Log.e("---电视节目---点击",position+"");
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(VideoOnDemandActivity.this, "长恩了"+position, Toast.LENGTH_SHORT).show();
Log.e("---电视节目---长摁",position+"");
}
});
}
}}

Bean代码:

public class VideoListBean {
private String Id = "";
private String Img = "";
private String Name = "";
private int imageId;
private String selectTag = "";
private int selectNum = 1;
public VideoListBean(JSONObject item) {
this.Id = item.optString("Id");
this.Img = item.optString("Img");
this.Name = item.optString("Name");
this.imageId = item.optInt("imageId");
this.selectTag = item.optString("selectTag");
this.selectNum = item.optInt("selectNum");
}
public VideoListBean(String name, int imageId) {
this.Name = name;
this.imageId = imageId;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getImg() {
return Img;
}
public void setImg(String img) {
Img = img;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getSelectTag() {
return selectTag;
}
public void setSelectTag(String selectTag) {
this.selectTag = selectTag;
}
public int getSelectNum() {
return selectNum;
}
public void setSelectNum(int selectNum) {
this.selectNum = selectNum;
}
}

Adapter代码:

public class VideoListAdapter extends RecyclerView.Adapter<VideoListViewHolder> {
private LayoutInflater mInflater;
private Context mContext;
private ArrayList<VideoListBean> mDatas;
private String mAdapterType;
private int productnum = 1;
private Account mAccount;
//    private VolleyHelper mHttpHelper;
double res;
int ListSize = 0;
private String itmeViewTag;
 int choosepos = 0;
List<VideoListBean> mSubmitOrderList = null;
public interface OnItemClickListener {
void onItemClick(View view, int position, ArrayList<VideoListBean> msg);
void onItemLongClick(View view, int position);
}
private OnItemClickListener mOnItemClickListener;
public void setmOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
public VideoListAdapter(Context context, ArrayList<VideoListBean> datas, String adapterType) {
this.mContext = context;
this.mDatas = datas;
this.itmeViewTag = adapterType;
mInflater = LayoutInflater.from(context);
}
public void updateData(ArrayList<VideoListBean> Datas) {
if (Datas != null) {
mDatas = Datas;
notifyDataSetChanged();
}
}
public void removeorder() {
for (int i = 0; i < mDatas.size(); i++) {
mDatas.remove(i);
}
notifyDataSetChanged();
}
@Override
public VideoListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
mSubmitOrderList = new ArrayList<>();
view = mInflater.inflate(R.layout.item_video, parent, false);
VideoListViewHolder viewHolder = new VideoListViewHolder(view);
viewHolder.setIsRecyclable(false);
return viewHolder;
}
@Override
public void onBindViewHolder(final VideoListViewHolder holder, final int position) {
 if (choosepos == position){
holder.fruitImage.setImageResource(R.drawable.ic_video);
}else {
holder.fruitImage.setImageResource(R.drawable.ic_launcher);
}
try {
holder.fruitName.setText(mDatas.get(position).getName());
holder.mDeleteImg.setVisibility(View.INVISIBLE);
holder.mNumTv.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
if (mOnItemClickListener != null)
{
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choosepos = position;
notifyDataSetChanged();
Log.e("Item点击", "Item点击" + position);
mOnItemClickListener.onItemClick(holder.itemView, position, mDatas);
//                    mOnItemClickListener.onItemLongClick(holder.itemView, position, mDatas.get(position).getName());
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mOnItemClickListener.onItemLongClick(holder.itemView,position);
//                    return false;
return true;
}
});
}
}
@Override
public int getItemCount() {
/**
* 这里在Account存储购物车数量,用来判断购物车是否为空,如果为空显示刷新页面
* */
try {
} catch (Exception e) {
e.printStackTrace();
}
return 23;
//        return mDatas.size();
}
}
class VideoListViewHolder extends RecyclerView.ViewHolder {
TextView mNumTv;
ImageView fruitImage;
TextView fruitName;
ImageView mDeleteImg;
public VideoListViewHolder(View itemView) {
super(itemView);
fruitImage=(ImageView)itemView.findViewById(R.id.item_food_img);
fruitName=(TextView) itemView.findViewById(R.id.item_food_name);
mDeleteImg = itemView.findViewById(R.id.delete_img);
mNumTv = itemView.findViewById(R.id.select_num_tv);
}
}

加油!!!

人已赞赏
Android文章

Android开发Application 无法跳转到 Activity

2020-3-8 14:28:42

Android文章

Android TV webview禁止自动获取焦点 遥控器按键监听 dispatchKeyEvent

2020-3-8 16:25:34

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索