gpt4 book ai didi

java - 获取recyclerview中的按钮状态

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:14 26 4
gpt4 key购买 nike

我正在使用回收 View ,并且我尝试仅在选定状态上设置按钮一次。

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`

我的问题是如何让上一个按钮 View 将其设置为 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)

最佳答案

我找到了解决方案

我应该首先将回收 View 实例从外部类传递到我的自定义适配器

内部回收 View 适配器很薄,我可以通过它在回收 View 中的位置获取任何 View 并将其转换到 View 持有者,然后可以使用持有者做任何事情

这里是代码:

外部类:

        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);

}

关于java - 获取recyclerview中的按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com