gpt4 book ai didi

java - RecyclerView onClick 获取项目的位置

转载 作者:行者123 更新时间:2023-11-29 09:52:55 28 4
gpt4 key购买 nike

我有一个 RecyclerView,它包含一堆卡片(从网络获取数据,然后填充卡片)。获取数据后,我的卡片可以正确形成,但现在我希望在单击每张卡片时设置一个监听器。理想情况下,在每张卡片被点击 后,一个新的intent 将被调用,其中包含有关该卡片数据的更多详细信息。

这是我初始化适配器的地方。 arrList 是一个 ArrayList 已经从另一个类中填充了

private void initializeAdapter() {
RecycleViewAdapter adapter = new RecycleViewAdapter(arrList);
rv.setAdapter(adapter);
}

这是另一个持有卡片的类:

public class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

CardView cv;
TextView crimeName,crimeID, crimeAddress, crimeDate, crimeWeapon;
ImageView crimePhoto;

PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);

crimeName = (TextView) itemView.findViewById(R.id.crime_name);
crimeID = (TextView) itemView.findViewById(R.id.crime_id);
crimeAddress = (TextView) itemView.findViewById(R.id.crime_address);
crimeDate = (TextView) itemView.findViewById(R.id.crime_date);
crimeWeapon = (TextView) itemView.findViewById(R.id.crime_weapon);
crimePhoto = (ImageView)itemView.findViewById(R.id.crime_photo);

itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
// WHAT DO I CALL HERE TO GET THE INDEX OF THE ITEM CLICKED ON ?? //
Log.d("app", "clicked");

}
}

上面的 onClick 应该转到这个类(通过 Intent )并传递索引:

    import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


public class CrimeDetails extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_details);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.crime_details, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_crime_details, container, false);
return rootView;
}
}
}

如何获取卡片的点击位置?

谢谢

最佳答案

请在 onCreateVH 方法中但在 onBindViewHolder 中设置 onClickListener

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((PersonViewHolder)holder).yourRootLayout.setOnClickListener(getPersonClickListener(position));
}

View.OnClickListener getPersonClickListener(final int position) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
//TODO whatever you want with position
}
};
}

编辑:tyczj是对的。在 onBindViewHolder 中绑定(bind) Clicklistener 是一个非常简单但可能不是最好的解决方案 - 但它会起作用!

另一种方法是使用 Viewholder 本身的 getAdapterPosition() 方法并将其绑定(bind)到 onCreateViewHolder 中:

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.yourLayoutXml, parent, false);
final ViewHolder holder = new PersonViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = holder.getAdapterPosition();
//check if position exists
if (position != RecyclerView.NO_POSITION) {
//TODO whatever you want
}
}
});
return holder;
}

关于java - RecyclerView onClick 获取项目的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31053235/

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