我有一个RecyclerView
,其中包含JSON
中本地CardView
的数据。我需要在单击某个或某个项目时更改所选项目(更改背景项目选中或突出显示)(如在线应用程序中编辑)但不使用按钮或longpress
。但我不想使用StateListDrawable
或(使用XML)因为我有一些JSON
数据需要稍后处理。
我需要在我的Activity中使用一个状态,比如一个布尔值或某些东西来保存我点击的每个项目,但我再也没有任何解决方案了。我已阅读并尝试了一些教程,但它无法正常工作。现在低于我的活动:
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
@Override
public void OnRecyclerViewItemClicked(int position) {
boolean selectedItem = false;
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
@Override
public void OnRecyclerViewItemClicked(int position) {
/* ------ boolean variabel for adapter but still not work ----- */
JSONObject filteredtableList= null;
try {
filteredtableList= new JSONObject("response").getJSONObject("tTisch");
}
catch (JSONException e) {
e.printStackTrace();
}
if (recyclerView == null) {
try {
filteredtableList.has("true");
filteredtableList.put(status, true);
} catch (JSONException e) {
e.printStackTrace();
}
}
else {
try {
filteredtableList.put(status, true);
}
catch (JSONException e) {
e.printStackTrace();
}
}
adapter.updateData(filteredTableList);
//----------------------------------------------------
try {
Toast.makeText(TableActivity.this, filteredTableList.getJSONObject(position).getString("tischnr"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Adapter.java
public int lastCheckedPosition = -1;
.........
.........
.........
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position == lastCheckedPosition) {
holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
} else {
holder.itemView.setBackgroundResource(R.color.colorTableGreen);
}
try {
currItem = list.getJSONObject(position);
holder.txt_no_table.setText(currItem.getString("tischnr"));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return list.length();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
}
@Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
lastCheckedPosition = getAdapterPosition();
notifyItemRangeChanged(0,list.length());
}
}
JSON.json
..............
"t-tisch": [
{
"tischnr": 1,
"departement": 1,
"normalbeleg": 0,
"kellner-nr": 0,
"bezeich": "TABLE 01",
"roomcharge": false,
"betriebsnr": 0
},
{
"tischnr": 2,
"departement": 1,
"normalbeleg": 0,
"kellner-nr": 0,
"bezeich": "TABLE 02",
"roomcharge": false,
"betriebsnr": 0
},
............
这是我的活动看起来像
立即输出:
当我点击该项目时,它现在突出显示(感谢@Burhanuddin Rashid),但它仍然是我案例的一半。我只能选择一个项目(我无法选择多个/多个项目)。当我点击突出显示项目时,我需要再次取消选择。
编辑:我尝试在我的JSON(上面的JSON代码)中编写新对象和密钥。在我的逻辑中,它会为每个项目选择与否制作一个标志,但它仍然不起作用。
已编辑:这是我的日志错误:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.development_laptop.vhp_restotemp, PID: 3590
java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.put(java.lang.String, boolean)' on a null object reference
at com.example.development_laptop.vhp_restotemp.TableActivity$1.OnRecyclerViewItemClicked(TableActivity.java:74)
at com.example.development_laptop.vhp_restotemp.com.example.development_laptop.vhp_restotemp.recyclerview.source.Adapter$ViewHolder.onClick(Adapter.java:97)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:3)
您可以像这样应用setOnClickListener: -
View itemView;
Context context;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
this.context=parent.getContext();
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
holder.linearLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent,null));
}
else {
holder.linearLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}
}
});
}
答案 1 :(得分:3)
试试这个:
<强> onBindViewHolder:强>
if (position == lastCheckedPosition) {
holder.itemView.setBackgroundResource(R.drawable.ic_item_selection_background);
} else {
holder.itemView.setBackgroundResource(R.drawable.ic_item_deselection_background));
}
这是 ViewHolder :
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastCheckedPosition = getAdapterPosition();
notifyItemRangeChanged(0,getItemCount());
}
});
创建lastCheckedPosition
作为全局变量
答案 2 :(得分:2)
遵循以下准则:
将此代码放入onBindViewHolder()
方法中。
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final DataModel dataModel = arraylist.get(position);
final boolean isSelected = dataModel.isSelected();
if (isSelected) {
llParentLayout.setBackgroundColor(Color.Black);
} else {
llParentLayout.setBackgroundColor(Color.Green);
}
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
}
@Override
public void onClick(View itemView) {
final DataModel dataModel = arraylist.get(getAdapterPosition());
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(dataModel);
}
}
在您的活动中:
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
@Override
public void OnRecyclerViewItemClicked(DataModel dataModel) {
dataModel.setSelected(!dataModel.isSelected());
adapter.notifyDataSetChanged();
}
});
答案 3 :(得分:1)
我完全改变了我的代码,但现在它的功能很强大,我的代码看起来像这样:
我将Adapter
更改为通用适配器。它不会处理任何数据,但它会引发我的活动。
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
public int lastCheckedPosition = -1;
private static OnRecyclerViewItemClickedListener recyclerViewItemClickedListener;
public void setOnRecyclerViewClickedListener (OnRecyclerViewItemClickedListener l) {
recyclerViewItemClickedListener = l;
}
public interface OnRecyclerViewItemClickedListener {
void OnRecyclerViewItemClicked(int position);
void OnRecyclerViewItemBind(ViewHolder holder, int position);
int OnRecyclerViewItemCount();
}
public Adapter() {
super();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_empty, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
recyclerViewItemClickedListener.OnRecyclerViewItemBind(holder,position);
}
@Override
public int getItemCount() {
return recyclerViewItemClickedListener.OnRecyclerViewItemCount();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
}
@Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
}
}
}
我的Activity
变成了这样:
/*Add Click Listener*/
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
@Override
public void OnRecyclerViewItemClicked(int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
if (currTable.has("selected")) {
currTable.put("selected", !currTable.getBoolean("selected"));
} else {
currTable.put("selected",true);
}
adapter.notifyItemChanged(position);
} catch (JSONException e) {
e.printStackTrace();
}
try
{
Toast.makeText(TableActivity.this, filteredTableList.getJSONObject(position).getString("tischnr"), Toast.LENGTH_SHORT).show();
}
catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void OnRecyclerViewItemBind(Adapter.ViewHolder holder, int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
holder.txt_no_table.setText(currTable.getString("tischnr"));
int queasy33Index = ProgramMethod.getJSONArrayIndex(queasy33List,"number2", currTable.getInt("tischnr"));
if (queasy33Index >= 0) {
holder.txt_no_table.setText(holder.txt_no_table.getText() + "-" + queasy33List.getJSONObject(queasy33Index).getString("key"));
}
if (currTable.has("selected") && currTable.getBoolean("selected")) {
holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
} else {
holder.itemView.setBackgroundResource(R.color.colorTableGreen);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
现在点击监听器保存任何状态并保存到我的JSON文件中的布尔值,如下所示:
感谢大家的建议和很好的答案:)
答案 4 :(得分:0)
尝试以下代码
内
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
把
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_image_and_text, parent, false);
v.setOnClickListener(new MyOnClickListener());
在任何你想要的地方创建这个类
class MyOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int itemPosition = recyclerView.indexOfChild(v);
Log.e("Clicked and Position is ",String.valueOf(itemPosition));
}
}
之前我读过有更好的方法,但我喜欢这种方式很容易而且不复杂。
答案 5 :(得分:0)
以上都是非常好的答案,但我相信你会选择这个 这为您提供了比其他人更多的灵活性
document.getElementById('divElem').scrollIntoView();