英文:
How can I get the position of the list item clicked while using Firebase RecyclerAdapter
问题
我正在使用 Firebase RecyclerAdapter,而不是普通的 RecyclerAdapter。我想存储被点击的列表项的位置。
下面是 HomeFragment 的代码:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
private RecyclerView recyclerView;
private ProductAdapter adapter;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = v.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
String u = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseRecyclerOptions<Product> options = new FirebaseRecyclerOptions.Builder<Product>()
.setLifecycleOwner(this)
.setQuery(FirebaseDatabase.getInstance().getReference().child(u).child("Products"), Product.class)
.build();
adapter = new ProductAdapter(options);
recyclerView.setAdapter(adapter);
return v;
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
}
ProductAdapter 类的代码如下:
package com.example.prj;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.squareup.picasso.Picasso;
public class ProductAdapter extends FirebaseRecyclerAdapter<Product, ProductAdapter.ProductViewHolder> {
private static final String TAG = "ProductAdapter";
ImageView btn_stock;
public ProductAdapter(@NonNull FirebaseRecyclerOptions<Product> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Product model) {
holder.pname.setText(model.getPname());
holder.price.setText(model.getPrice());
holder.description.setText(model.getDescription());
Picasso.get().load(model.getImage()).into(holder.image);
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_row, parent, false);
return new ProductViewHolder(view);
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView pname, price, description;
ImageView image;
public ProductViewHolder(@NonNull final View itemView) {
super(itemView);
pname = itemView.findViewById(R.id.product_list_name);
price = itemView.findViewById(R.id.product_list_price);
description = itemView.findViewById(R.id.product_list_description);
image = itemView.findViewById(R.id.product_list_image);
}
}
}
我想利用点击的列表项位置来使列表项可展开。用户点击列表项,列表项会展开并显示有关该项的更多详细信息。如果有人对此有解决方案,请在这方面给予帮助。
英文:
I am using Firebase RecyclerAdapter rather than using the normal RecyclerAdapter. I wanted to store the position of the list Item which is clicked.
The code for the HomeFragment is below
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
private RecyclerView recyclerView;
private ProductAdapter adapter;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = v.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
String u = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseRecyclerOptions<Product> options = new FirebaseRecyclerOptions.Builder<Product>()
.setLifecycleOwner(this)
.setQuery(FirebaseDatabase.getInstance().getReference().child(u).child("Products"), Product.class)
.build();
adapter = new ProductAdapter(options);
recyclerView.setAdapter(adapter);
return v;
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
}
The code for the ProductAdapter class is given below
package com.example.prj;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.squareup.picasso.Picasso;
public class ProductAdapter extends FirebaseRecyclerAdapter<Product, ProductAdapter.ProductViewHolder> {
private static final String TAG = "ProductAdapter";
ImageView btn_stock;
public ProductAdapter(@NonNull FirebaseRecyclerOptions<Product> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Product model) {
holder.pname.setText(model.getPname());
holder.price.setText(model.getPrice());
holder.description.setText(model.getDescription());
Picasso.get().load(model.getImage()).into(holder.image);
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_row, parent, false);
return new ProductViewHolder(view);
}
class ProductViewHolder extends RecyclerView.ViewHolder{
TextView pname, price, description;
ImageView image ;
public ProductViewHolder(@NonNull final View itemView) {
super(itemView);
pname = itemView.findViewById(R.id.product_list_name);
price = itemView.findViewById(R.id.product_list_price);
description = itemView.findViewById(R.id.product_list_description);
image = itemView.findViewById(R.id.product_list_image);
}
}
Using the position of the list item which is clicked I want to make the list item expandable.Like the users clicks on the list item and the list item expands and shows some more details about that item..If someone has the solution for that please help me in that also..
答案1
得分: 0
在onBindViewHolder()
中设置项目点击监听器:
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Product model) {
// ...
// ...
// ...
// 在这里设置项目的点击事件
holder.view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
// 在这里使用传递给onBindViewHolder的位置做一些操作
// 如果点击了项目1,位置为0
// 如果点击了项目2,位置为1
// ..............
Log.d("POSITION" , String.valueOf(position));
}
});
}
在您的ViewHolder中添加view
参数:
class ProductViewHolder extends RecyclerView.ViewHolder{
TextView pname, price, description;
ImageView image ;
// 在这里添加
View view;
public ProductViewHolder(@NonNull final View itemView) {
super(itemView);
// 在这里设置
view = itemView;
// ...
// ...
}
英文:
In onBindViewHolder()
set the item click listener:
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Product model) {
.....
.....
....
//here set the click of the item
holder.view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//here do something with position that is passed in the onBindViewHolder
//if you clicked item 1 the position is 0
//if you clicked item 2 the position is 1
..............
Log.d("POSITION" , String.valueOf(position));
}
});
In your Viewholder add the view
parameter
class ProductViewHolder extends RecyclerView.ViewHolder{
TextView pname, price, description;
ImageView image ;
//here
View view;
public ProductViewHolder(@NonNull final View itemView) {
super(itemView);
//set it here
view = itemView;
......
......
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论