我正在尝试在Recyclerview单个项目上实现Onclicklistner。 我有点击单个项目时添加的功能 功能是
这是我的RecyclerAdapter
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.awfis.vms.R;
import com.awfis.vms.model.DataModel;
import java.util.ArrayList;
import java.util.List;
import static com.android.volley.VolleyLog.TAG;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
LinearLayout mLinearLayout;
Context context;
List<DataModel> mData;
public RecyclerViewAdapter(Context context, List<DataModel> mData) {
this.context = context;
this.mData = mData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.typeofGuest.setText(mData.get(position).getText());
holder.guestImage.setImageResource(mData.get(position).getDrawable());
holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mLinearLayout.setBackgroundColor(R.drawable.border_view_inpressed);
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
LinearLayout mLinearLayout;
TextView typeofGuest;
ImageView guestImage;
boolean click = true;
int[] myImageList = new int[]{R.drawable.guest, R.drawable.meetingroom_booking, R.drawable.centertour, R.drawable.housekeeping, R.drawable.delivery, R.drawable.vendor};
int[] myImageList_one = new int[]{R.drawable.guest_two, R.drawable.meetingroom_booking_two, R.drawable.centertour_two, R.drawable.housekeeping_two, R.drawable.deliverytwo, R.drawable.vendor_two};
public MyViewHolder(View itemView) {
super(itemView);
mLinearLayout=itemView.findViewById(R.id.recycleritem_layout);
typeofGuest = itemView.findViewById(R.id.guest_type);
guestImage = itemView.findViewById(R.id.guest_pic);
typeofGuest.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (click) {
mLinearLayout.setBackgroundColor(Color.parseColor("#F44336"));
typeofGuest.setTextColor(Color.parseColor("#F44336"));
guestImage.setImageResource(myImageList[getPosition()]);
if (view.getId() == typeofGuest.getId()) {
if(getAdapterPosition()==1)
Toast.makeText(view.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
click = false;
} else {
typeofGuest.setTextColor(Color.parseColor("#000000"));
mLinearLayout.setBackgroundColor(Color.parseColor("#000000"));
guestImage.setImageResource(myImageList_one[getPosition()]);
click = true;
}
}
}
}
}
我的模特课
public class DataModel {
public String text;
public boolean isSelected;
public void setSelected(boolean selected) {
isSelected = selected;
}
public int drawable;
public DataModel(){
}
public DataModel(String text, int drawable) {
this.text = text;
this.drawable = drawable;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getDrawable() {
return drawable;
}
public void setDrawable(int drawable) {
this.drawable = drawable;
}
}
这是我的活动,我必须显示RecyclerView
public class AddVisitorActivity extends BaseActivity {
List<DataModel>lsData;
int spanCount = 3; // 3 columns
int spacing = 50; // 50px
boolean includeEdge = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_visitor);
lsData= new ArrayList<>();
lsData.add(new DataModel("Guest",R.drawable.guest_two));
lsData.add(new DataModel("Metting Room Booking",R.drawable.meetingroom_booking_two));
lsData.add(new DataModel("Center Tour",R.drawable.centertour_two));
lsData.add(new DataModel("House Keeping",R.drawable.housekeeping_two));
lsData.add(new DataModel("Delivery",R.drawable.deliverytwo));
lsData.add(new DataModel("Vendor",R.drawable.vendor_two));
RecyclerView mRecyclerview= findViewById(R.id.rvNumbers);
RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(this,lsData);
mRecyclerview.setLayoutManager(new GridLayoutManager(this,3));
mRecyclerview.setAdapter(mAdapter);
mRecyclerview.addItemDecoration(new SpacesItemDecoration(spanCount, spacing, includeEdge));
}
}
我的recyclerView项目xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/_75sdp"
android:id="@+id/recycleritem_layout"
android:layout_height="@dimen/_70sdp"
android:background="@drawable/borde_view"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/guest_pic"
android:layout_width="@dimen/_27sdp"
android:layout_height="@dimen/_27sdp"
android:layout_marginTop="@dimen/_10sdp"
android:src="@drawable/guest" />
<TextView
android:id="@+id/guest_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:gravity="center"
android:text="@string/metting_room_booking"
android:textSize="@dimen/_10sdp" />
</LinearLayout>
</LinearLayout>
我的添加访问者活动xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/layout_add_visitor"
android:layout_height="match_parent"
android:background="@drawable/mainbg"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="@dimen/_90sdp"
android:layout_height="@dimen/_49sdp"
android:layout_centerInParent="true"
android:background="@drawable/logo" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/_10sdp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="@dimen/_350sdp"
android:layout_gravity="center"
android:elevation="@dimen/_5sdp"
app:cardCornerRadius="@dimen/_10sdp">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/addvisitor_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:text="@string/add_new_visitor"
android:textColor="@color/Textcolor"
android:textSize="@dimen/_15sdp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/addvisitor_text"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:text="@string/visitor_type"
android:textColor="@color/Textcolor"
android:textSize="@dimen/_7sdp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvNumbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/_7sdp"
android:paddingRight="@dimen/_7sdp"
android:layout_marginTop="@dimen/_60sdp" />
<LinearLayout
android:id="@+id/select_host"
android:layout_width="@dimen/_270sdp"
android:layout_height="@dimen/_50sdp"
android:layout_below="@id/rvNumbers"
android:layout_marginLeft="@dimen/_7sdp"
android:layout_marginRight="@dimen/_7sdp"
android:layout_marginTop="@dimen/_15sdp"
android:background="@drawable/borde_view"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3"
android:baselineAligned="false">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/_10sdp"
android:layout_marginTop="@dimen/_5sdp"
android:paddingLeft="@dimen/_9sdp"
android:text="@string/select_host"
android:textSize="@dimen/_7sdp" />
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_3sdp"
android:hint="@string/jhon_smith"
android:paddingStart="@dimen/_9sdp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:weightSum="1">
<Button
android:gravity="center"
android:layout_width="@dimen/_44sdp"
android:layout_height="@dimen/_26sdp"
android:background="@color/CMbuttonColor"
android:text="@string/cm"
android:textColor="@color/colorWhite" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="@dimen/_270sdp"
android:layout_height="@dimen/_35sdp"
android:layout_below="@id/select_host"
android:layout_marginLeft="@dimen/_7sdp"
android:layout_marginRight="@dimen/_7sdp"
android:layout_marginTop="@dimen/_15sdp"
android:background="@drawable/splash_bg"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/MainLoginButtonColor"
android:text="@string/next"
android:textColor="@color/colorWhite"
android:textSize="@dimen/_10sdp"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
如果项目数据不足以决定显示哪个,你可以考虑使用一个包装器ViewData类来保存项目数据和视图状态,如isButtonShown,imageUrl,......所以基于ViewData类,你可以决定ViewHolder的UI