我刚刚创建了一个回收站视图,其中包含卡片,我想setOnCLick
为此位置视图的吐司面包加一个位置项目单击,因为这是我第一次使用回收站视图,所以我始终使用列表视图setOnItemClickListner
如何在Gridview
中添加这样的吐司位置?
我的适配器类
grid_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked letter: " + letters_list[position], Toast.LENGTH_SHORT).show();
}
});
MainActivity类
public class MainActivity extends AppCompatActivity {
public static long size;
int numberOfColumns = 5;
MyRecyclerViewAdapter adapter;
AdapterProgressBar adapterProgressBar;
ProgressBar progressBar;
int currentItems, totalItems, scrollOutItems;
Boolean isScrolling = false;
GridLayoutManager manager;
ArrayList list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
final String data[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48","49","50"};
// set up the RecyclerView
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers);
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
adapter = new MyRecyclerViewAdapter(this,data);
//adapter.setClickListener((MyRecyclerViewAdapter.ItemClickListener) this);
recyclerView.setAdapter(adapter);
//progress bar --> scroll
manager = new GridLayoutManager(this, 5);
progressBar = (ProgressBar) findViewById(R.id.progress);
list = new ArrayList(Arrays.asList(data));
adapterProgressBar = new AdapterProgressBar(list,this);
recyclerView.setAdapter(adapterProgressBar);
//recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
recyclerView.setLayoutManager(manager);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
isScrolling = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentItems = manager.getChildCount();
totalItems = manager.getItemCount();
scrollOutItems = manager.findFirstCompletelyVisibleItemPosition();
//Size of array
size = list.size();
//Toast.makeText(MainActivity.this, "newnumber = " + newNumber, Toast.LENGTH_SHORT).show();
if(isScrolling && currentItems + scrollOutItems == totalItems){
//fetch data
isScrolling = false;
fitchData(size);
}
}
});
/*gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked letter: " + data[position], Toast.LENGTH_SHORT).show();
}
});*/
//Ends OnCreate
}
//This function to add numbers after scroll
private void fitchData(final long size){
progressBar.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for (long i=size+1 ; i<size+51 ; i++){
list.add( i + "");
adapterProgressBar.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
//Toast.makeText(MainActivity.this, "size = " + size , Toast.LENGTH_SHORT).show();
}
}, 5000);
}
}
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private String[] mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
//progress
ArrayList<String> data;
Context context;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, String[] data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
// inflates the cell layout from xml when needed
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
//LayoutInflater inflater = LayoutInflater.from(context);
return new ViewHolder(view);
}
// binds the data to the TextView in each cell
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position)
{
holder.myTextView.setText(mData[position]);
}
// total number of cells
@Override
public int getItemCount() {
return mData.length;
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.info_text);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Toast.makeText(context, "Clicked Positionnnnn", Toast.LENGTH_SHORT).show();
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData[id];
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
答案 0 :(得分:0)
在自定义适配器中定义接口时,您应该在活动中实现此接口,如下所示:
public class MainActivity extends AppCompatActivity implements
MyRecyclerViewAdapter. ItemClickListener{
//......
adapter = new MyRecyclerViewAdapter(this,data);
adapter.setClickListener(this);
//......
@Override
public void onItemClick(View view, int position) {
Toast.makeText(context, "Clicked:"+ String.valueOf(position),
, Toast.LENGTH_SHORT).show();
}
//......
}
答案 1 :(得分:0)
“回收者”视图直接以onBindViewHolder
方法在itemView上提供点击侦听器。
您可以将这段代码直接放入回收站适配器的 onBindViewHolder 方法中。
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context c = v.getContext();
Toast.makeText(c,"Hello
Javatpoint",Toast.LENGTH_SHORT);
}
});
但是,如果要使用下面的接口来实现它,请参见以下代码段:
public interface OnItemClickListener
{
void onClick(int position);
}
Activity可以实现此接口,或实例化匿名内部类。因此您的适配器可能如下所示:
public class MyRecyclerAdapter extends RecyclerView.Adapter
{
public final OnItemClickListener listener;
public MyRecyclerAdapter(OnItemClickListener listener)
{
this.listener = listener;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
/** Here is you code**/
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(position);
}
});
}
}
}