我正在OnItemClickListener接口的OnItemClicked(int position,view v)方法中传递position和view。现在,在我的ViewReceptionistActivity中,我要检查单击了哪个项和哪个子部分。如果用户单击图像,则执行任务A,如果用户单击名称,则执行任务B,依此类推。
下面是我的布局文件-viewreceptionist_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
cardElevation="5dp"
contentPadding="10dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="12"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@drawable/card_background">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="2">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/viewReceptionistImage"
android:layout_width="70dp"
android:layout_height="70dp"
app:civ_border_width="2dp"
android:layout_gravity="center"
app:civ_border_color="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9">
<TextView
android:id="@+id/textViewViewReceptionistName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="18dp"
android:layout_marginTop="5dp"
android:textColor="@color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewViewReceptionistEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:textColor="@color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewViewReceptionistMobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No."
android:textSize="15dp"
android:layout_marginTop="5dp"
android:textColor="@color/black"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/viewReceptionistGender"
android:layout_width="40dp"
android:layout_height="40dp"
app:civ_border_width="2dp"
android:layout_gravity="center"
app:civ_border_color="@color/black" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
这是我的适配器-ViewReceptionistAdapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class ViewReceptionistAdapter extends RecyclerView.Adapter<ViewReceptionistAdapter.ViewReceptionistHolder>
{
Context context;
int resource;
ArrayList<Receptionist> objects;
OnItemClickListener listener;
public interface OnItemClickListener
{
void OnItemClicked(int position, View v);
}
public void setOnItemClickListener(OnItemClickListener listener)
{
this.listener = listener;
}
public ViewReceptionistAdapter(Context context, int resource, ArrayList<Receptionist> objects)
{
this.context = context;
this.resource = resource;
this.objects = objects;
}
@Override
public ViewReceptionistHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(context).inflate(resource, parent, false);
ViewReceptionistHolder viewReceptionistHolder = new ViewReceptionistHolder(view);
return viewReceptionistHolder;
}
@Override
public void onBindViewHolder(ViewReceptionistHolder holder, int position)
{
Receptionist receptionist = objects.get(position);
holder.tvViewReceptionistName.setText(receptionist.name);
holder.tvViewReceptionistEmail.setText(receptionist.email);
holder.tvViewReceptionistMobile.setText(receptionist.mobile);
String url = receptionist.imageUrl;
Picasso.get().load(url).into(holder.viewReceptionistImage);
String gender;
gender = receptionist.gender;
if(gender.equals("Male"))
{
holder.viewReceptionistgender.setImageResource(R.drawable.male_icon);
}
else
{
holder.viewReceptionistgender.setImageResource(R.drawable.female_icon);
}
}
@Override
public int getItemCount()
{
return objects.size();
}
public class ViewReceptionistHolder extends RecyclerView.ViewHolder
{
public TextView tvViewReceptionistName;
public TextView tvViewReceptionistEmail;
public TextView tvViewReceptionistMobile;
public CircleImageView viewReceptionistImage;
public CircleImageView viewReceptionistgender;
public ViewReceptionistHolder(final View itemView)
{
super(itemView);
tvViewReceptionistName = itemView.findViewById(R.id.textViewViewReceptionistName);
tvViewReceptionistEmail = itemView.findViewById(R.id.textViewViewReceptionistEmail);
tvViewReceptionistMobile = itemView.findViewById(R.id.textViewViewReceptionistMobile);
viewReceptionistImage = itemView.findViewById(R.id.viewReceptionistImage);
viewReceptionistgender = itemView.findViewById(R.id.viewReceptionistGender);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(listener != null)
{
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
{
listener.OnItemClicked(position, v);
}
}
}
});
}
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
public class ViewReceptionistActivity extends AppCompatActivity
{
RecyclerView recyclerView;
ArrayList<Receptionist> receptionistsList;
ViewReceptionistAdapter viewReceptionistAdapter;
FirebaseFirestore firestore;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_receptionist);
getSupportActionBar().setTitle("View Receptionist");
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
firestore = FirebaseFirestore.getInstance();
receptionistsList = new ArrayList<Receptionist>();
Query query = firestore.collection("Receptionist").orderBy("name", Query.Direction.ASCENDING);
query.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e)
{
for(DocumentSnapshot documentSnapshot: documentSnapshots)
{
Receptionist receptionist = documentSnapshot.toObject(Receptionist.class);
receptionistsList.add(receptionist);
viewReceptionistAdapter.notifyDataSetChanged();
}
}
});
viewReceptionistAdapter = new ViewReceptionistAdapter(this, R.layout.viewreceptionist_item, receptionistsList);
viewReceptionistAdapter.setOnItemClickListener(new ViewReceptionistAdapter.OnItemClickListener() {
@Override
public void OnItemClicked(int position, View v)
{
Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + " " + v, Toast.LENGTH_SHORT).show();
//HERE I WANT TO CHECK IF USER CLICKS AN IMAGE OR NAME OR EMAIL...
//IF USER CLICKS IMAGE - PERFORM TASK A
//IF USSER CLICKS NAME - PERFORM TASK B...AND SO ON
}
});
LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(viewReceptionistAdapter);
}
}
您最好使用每个视图的方法扩展click界面,以便为click事件添加一个反应:
public interface OnItemClickListener
{
void OnItemImageClicked(int position, View v);
void OnItemNameClicked(int position, View v);
}
在你的观赏者中:
viewReceptionistImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(listener != null)
{
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
{
listener.OnItemImageClicked(position, v);
}
}
}
});
tvViewReceptionistName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(listener != null)
{
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
{
listener.OnItemNameClicked(position, v);
}
}
}
});
然后,在您的ViewReceptionistActivity中,您只需实现接口并重写方法(因为它们会很多)。然后在适配器中设置侦听器,如下所示:
viewReceptionistAdapter.setOnItemClickListener(this);
viewReceptionistAdapter.setOnItemClickListener(new ViewReceptionistAdapter.OnItemClickListener() {
@Override
public void OnItemImageClicked(int position, View v)
{
Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + "Image View: " + v, Toast.LENGTH_SHORT).show();
}
@Override
public void OnItemNameClicked(int position, View v)
{
Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + " TextView: " + v, Toast.LENGTH_SHORT).show();
}
});
--lesson.java --list_item.xml
我目前正在开发一个应用程序,可以在用户手机上找到所有MP3,然后将它们放入列表中。这是非常好的工作,非常快,即使有很多歌曲。现在,我为列表中的每个项目填充一个新列表,然后将其显示在我的recyclerview中。问题是,我的手机上有700首歌曲,这在相当长的一段时间内会阻塞UI线程。 现在,我想使用回收站视图不将列表中的所有项目一次加载到对象中,而是仅在它们即将显示时加载-但我不知道如何做到这一点
这是我第一次尝试实现。我在和类之间实现了一个接口来处理UI元素(按钮)上的点击事件。我在获取对发生单击的的引用时遇到问题。我可以单击列表中第一个项目的,但是我定义的操作是在我的回收器视图中的最后一个上执行的。我终于想出了通过我的界面传递这个位置,但是我很难理解如何从它那里获得对那个查看者的引用。
我必须滚动到recyclerview中的特定项目。 首先,我需要通过匹配从活动传递给适配器的字符串来获取项的位置。然后聚焦到该项目的位置。 例如,在适配器中有一个存储了EventID的TextView,我将把一个字符串从activity传递给适配器,使其与EventID匹配,然后获取该项目的位置,并将焦点/滚动设置到该特定位置。我在适配器中定义了一个方法来获取项的位置,但我不知道如何从活动中调用它
问题内容: 假设此页面的代码: 会话托管bean的代码: 在(1)中,我想获取用户单击的元素的ID。我可以在lstIdSelectedItems数组列表中看到用户选择的所有元素的ID,但是如何获取用户单击的元素的ID? 我试图在selectManyCheckbox内使用f:attribute标记,但是当在支持的bean中调用ajax侦听器方法时,该属性不在组件映射中。我已经用过了,但是不起作用:
在recyclerView中获取已点击项目的位置,我在recyclerView中创建cardview和链接 我想获取位置,将开关盒设置为每个卡视图的意向活动 当我点击card view I时,他会打开另一个页面,当你点击card view 2时,他会打开另一个页面。 看看我的密码 在recyclerView中获取已点击项目的位置,我在recyclerView中创建cardview和链接 我想获取位