我正在使用Recycle view
而我正在尝试将button
设置为选定状态一次。
button 1 : when clicked set is as true .
button2 : when clicked set `button 1` to `false` state and then set `button 2` to `true`
button3: when clicked set `button 2` to `false` state and then set `button 3` to `true`
我的问题是如何获取上一个按钮视图将其设置为false?
public class MyCustomAdapterPackages extends RecyclerView.Adapter<MyCustomAdapterPackages.CustomViewHolderPackages> {
private ArrayList<Packages> packages;
private Context mContext;
int id=-1;
public MyCustomAdapterPackages(Context context, ArrayList<Packages> packages) {
this.packages = packages;
this.mContext = context;
}
@Override
public CustomViewHolderPackages onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.package_row_item, null);
CustomViewHolderPackages viewHolder = new CustomViewHolderPackages(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final MyCustomAdapterPackages.CustomViewHolderPackages holder, final int position) {
final Packages pa = packages.get(position);
holder.PackageButton.setText(pa.getPackageName());
holder.PackagePrice.setText(String.valueOf(pa.getPackagePrice()));
holder.PackageButton.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tab_bg_selector));
holder.PackageButton.setId(position);
holder.PackageButton.setTag(id);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if(id<0){
view.setSelected(true);
id=view.getId();
}
else{
if(((Button)holder.getTag(id)).isSelected()) {
view.findViewById(id).setSelected(false);
view.setSelected(true);
id=view.getId();
}
else{
id=view.getId();
view.setSelected(true);
}
}
}
};
//Handle click event on both title and image click
holder.PackageButton.setOnClickListener(clickListener);
// holder.PackagePrice.setOnClickListener(clickListener);
holder.PackageButton.setTag(packages.get(position));
// holder.PackagePrice.setTag(packages.get(position));
// holder.viewGroup.addView(ll);
}
@Override
public int getItemCount() {
return (null != packages ? packages.size() : 0);
}
public class CustomViewHolderPackages extends RecyclerView.ViewHolder {
protected Button PackageButton;
protected TextView PackagePrice;
public CustomViewHolderPackages(View view) {
super(view);
this.PackageButton = (Button) view.findViewById(R.id.PackageButton);
this.PackagePrice = (TextView) view.findViewById(R.id.PackagePrice);
}
}
}
记录错误:
Process: hesham.bestdealsoft.topline.elitedishes, PID: 15922
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.View.isSelected()' on a null object reference
at hesham.bestdealsoft.topline.elitedishes.MyCustomAdapterPackages$1.onClick(MyCustomAdapterPackages.java:75)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
答案 0 :(得分:0)
我找到了解决方案
我应该首先将外部类的循环视图实例传递给我的自定义适配器
瘦内部循环视图 - 适配器我可以通过它在循环视图中的位置获取任何视图并将其投射到视图持有者然后可以使用持有者做任何事情
这是代码:
出边课:
mRecyclerView2 = (RecyclerView) findViewById(R.id.list_Fragment);
mRecyclerView2.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView2.setLayoutManager(mLayoutManager);
mAdapter2 = new MyCustomAdapterPackages(this, PackagesList,mRecyclerView2);
mRecyclerView2.setAdapter(mAdapter2);
mAdapter2.notifyDataSetChanged();
客户端适配器类中的:
@Override
public void onBindViewHolder(final MyCustomAdapterPackages.CustomViewHolderPackages holder, final int position) {
final Packages pa = packages.get(position);
holder.PackageButton.setText(pa.getPackageName());
holder.PackagePrice.setText(String.valueOf(pa.getPackagePrice()));
holder.PackageButton.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tab_bg_selector));
holder.PackageButton.setId(position);
holder.PackageButton.setTag(id);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if(id<0){ // first time or default
view.setSelected(true);
id=view.getId();
}
else{
CustomViewHolderPackages row = (CustomViewHolderPackages)mRecyclerView.findViewHolderForAdapterPosition(id);
if( row.PackageButton.isSelected()) {
row.PackageButton.setSelected(false); // previous view
view.setSelected(true); // current view
id=view.getId();
}
else{
id=view.getId();
view.setSelected(true);
}
}
}
};
//Handle click event
holder.PackageButton.setOnClickListener(clickListener);
}