当我想点击回收站视图时,它应该打开另一个活动数据正在提取,但问题是当我点击recycleview时它什么也没显示。 我试过RecyclerView.OnItemTouchListener。但还没有成功。 请问你能解决这个问题,我哪里出错了
public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.MyViewHolder> {
List<AllRestaurant> restaurantList;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem, parent, false);
return new MyViewHolder(itemView);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
//public TextView long1, medium, small;
TextView t1,t2,t3,t4,t5;
ImageView logo;
public MyViewHolder(View view) {
super(view);
t1=(TextView)view.findViewById(R.id.t1);
t2=(TextView)view.findViewById(R.id.t2);
t3=(TextView)view.findViewById(R.id.t3);
t4=(TextView)view.findViewById(R.id.t4);
t5=(TextView)view.findViewById(R.id.t5);
logo=(ImageView) view.findViewById(R.id.image);
// final AllRestaurant items = getItem(position);
//long1.setText(items.getRestaurent_id());
// final NotifyUsers notifyUsers=new NotifyUsers(context);
// username.setText(items.getResponse_by());
// little.setText(items.g);
}
}
public RestaurantAdapter(List<AllRestaurant> restaurantList) {
this.restaurantList = restaurantList;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AllRestaurant restaurant = restaurantList.get(position);
RestaurentAddress restaurentAddress=restaurant.getRestaurentAddress();
RestaurentInfo restaurentInfo=restaurant.getRestaurentInfo();
RestaurentSetting restaurentSetting=restaurant.getRestaurentSetting();
User user=restaurant.getUser();
Zone zone=restaurant.getZone();
holder. t1.setText(restaurentInfo.getRestaurent_name());
holder.t2.setText(restaurentAddress.getRestaurent_address1());
holder. t3.setText("min_pur:"+restaurentSetting.getMinimum_purcase());
holder. t4.setText("Del In Min:"+restaurentSetting.getEstimate_delivery_time());
String imagename=restaurentInfo.getRestaurent_logo();
Bitmap bitmap=getImage(imagename);
holder.logo.setImageBitmap(bitmap);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getItemId(position);
}
});
/* long1.setText(restaurentInfo.getRestaurent_name());
little.setText(restaurentAddress.getCity_name());*/
}
@Override
public int getItemCount() {
return restaurantList.size();
}
public Bitmap getImage(String name){
Bitmap bitmap=null;
String url="http://ibookstay.com/oops/web/img/uploads/res_log/"+name;
OkhttpClient clientOkHttp=new OkhttpClient();
try {
byte image[]=new byte[0];
// image=clientOkHttp.execute(url).get();
image = clientOkHttp.execute(url).get();
if (image != null && image.length > 0){
bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
// imageView.setImageBitmap(bitmap);
// tv.setText("Total btytes download: "+ image.length);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
return bitmap;
}
}
这是我的片段文件:
public class RestaurantFragment extends Fragment {
RecyclerView recyclerView;
LinkedList<AllRestaurant> allRestaurants;
RestaurantAdapter allRestaurantAdapter;
MethodModel methodModel;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.restaurantlist, container, false);
methodModel=new MethodModel(getActivity());
allRestaurants=methodModel.getAllRestaurant();
allRestaurantAdapter=new RestaurantAdapter(allRestaurants);
recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
// set the adapter
recyclerView.setAdapter(allRestaurantAdapter);
return view;
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#d3d3d3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="desktop.oopsfood.Navigation"
tools:showIn="@layout/app_bar_navigation">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Search For Location"
android:id="@+id/locationget"
android:background="@drawable/rounded_border_edittext"
android:gravity="center"
android:layout_weight="0.99">
</EditText>
<requestFocus />
android:shadowRadius="2.0"
android:layout_weight=".3" />
</LinearLayout>
<!-- <ListView
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:dividerHeight="5dp"></ListView>-->
<FrameLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/container"
/>
</LinearLayout>
答案 0 :(得分:3)
Using Interface you can solve your problem:
public interface OnItemClickListener {
public void onItemClick(View view, int position, List<AllRestaurant> AllRestaurant);
}
在适配器类中实现接口,如下所示。
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//public TextView long1, medium, small;
TextView t1,t2,t3,t4,t5;
ImageView logo;
public MyViewHolder(View view) {
super(view);
t1=(TextView)view.findViewById(R.id.t1);
t2=(TextView)view.findViewById(R.id.t2);
t3=(TextView)view.findViewById(R.id.t3);
t4=(TextView)view.findViewById(R.id.t4);
t5=(TextView)view.findViewById(R.id.t5);
logo=(ImageView) view.findViewById(R.id.image);
// final AllRestaurant items = getItem(position);
//long1.setText(items.getRestaurent_id());
// final NotifyUsers notifyUsers=new NotifyUsers(context);
// username.setText(items.getResponse_by());
// little.setText(items.g);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition(), getData());
}
}
}
然后这样打电话:
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position, List<UserDetails> userDetailsList) {
Toast.makeText(getActivity(), "index --> " + position, Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
如果您想点击项目点击循环视图 首先,您需要创建以下类:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
protected OnItemClickListener listener;
private GestureDetector gestureDetector;
@Nullable
private View childView;
private int childViewPosition;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
this.gestureDetector = new GestureDetector(context, new GestureListener());
this.listener = listener;
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent event) {
childView = view.findChildViewUnder(event.getX(), event.getY());
childViewPosition = view.getChildPosition(childView);
return childView != null && gestureDetector.onTouchEvent(event);
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent event) {
// Not needed.
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
/**
* A click listener for items.
*/
public interface OnItemClickListener {
/**
* Called when an item is clicked.
*
* @param childView View of the item that was clicked.
* @param position Position of the item that was clicked.
*/
void onItemClick(View childView, int position);
/**
* Called when an item is long pressed.
*
* @param childView View of the item that was long pressed.
* @param position Position of the item that was long pressed.
*/
void onItemLongPress(View childView, int position);
}
/**
* A simple click listener whose methods can be overridden one by one.
*/
public static abstract class SimpleOnItemClickListener implements OnItemClickListener {
/**
* Called when an item is clicked. The default implementation is a no-op.
*
* @param childView View of the item that was clicked.
* @param position Position of the item that was clicked.
*/
public void onItemClick(View childView, int position) {
// Do nothing.
}
/**
* Called when an item is long pressed. The default implementation is a no-op.
*
* @param childView View of the item that was long pressed.
* @param position Position of the item that was long pressed.
*/
public void onItemLongPress(View childView, int position) {
// Do nothing.
}
}
protected class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
if (childView != null) {
listener.onItemClick(childView, childViewPosition);
}
return true;
}
@Override
public void onLongPress(MotionEvent event) {
if (childView != null) {
listener.onItemLongPress(childView, childViewPosition);
}
}
@Override
public boolean onDown(MotionEvent event) {
// Best practice to always return true here.
// http://developer.android.com/training/gestures/detector.html#detect
return true;
}
}
}
你的片段类:
public class RestaurantFragment extends Fragment implements RecyclerItemClickListener.OnItemClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout, container, false);
list.addOnItemTouchListener(new RecyclerItemClickListener(getActivity().getApplicationContext(), this));
.....
}
@Override
public void onItemClick(View childView, int position) {
// add the code for the click here
}
@Override
public void onItemLongPress(View childView, final int position) {
}
}
如果您需要任何帮助,请与我们联系
答案 2 :(得分:0)
这是我在recyclerview.addonItemTouchListener的帮助下创建的解决方案并创建了接口。
public class RestaurantFragment extends Fragment {
RecyclerView recyclerView;
LinkedList<AllRestaurant> allRestaurants;
RestaurantAdapter allRestaurantAdapter;
MethodModel methodModel;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.restaurantlist, container, false);
methodModel=new MethodModel(getActivity());
allRestaurants=methodModel.getAllRestaurant();
allRestaurantAdapter=new RestaurantAdapter(allRestaurants);
recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
// set the adapter
recyclerView.setAdapter(allRestaurantAdapter);
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
}
@Override
public void onLongClick(View view, int position) {
}
}));
return view;
}
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
public RecyclerTouchListener(Context context, RecyclerView recyclerView, ClickListener clicklistener){
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
Log.d("Performing Better", "onInterceptTouchEvent: "+e);
startActivity(new Intent(getActivity(), Details.class));
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
Log.d("On Touch Event Better", "onTouchEvent: "+e);
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
public static interface ClickListener {
public void onClick (View view, int position);
public void onLongClick(View view, int position);
}
}