经过很多天让Fragment实际显示我现在发现自己很难能够在recyclerview中点击项目。我已经阅读了很多关于如何做的帖子,但我必须遗漏一些东西。 首先让我说我的背景已经是VB开发人员多年了。
以下是我目前在Fragment Java中的内容。
package com.procatdt.stockright.activities;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.procatdt.stockright.R;
import com.procatdt.stockright.models.ItemsListModel;
import com.procatdt.stockright.utils.DatabaseMgr;
import java.util.List;
public class SelectItemFragment extends Fragment {
private RecyclerAdapter mRecyclerAdapter;
private String mParam1;
private Bundle args;
private RecyclerView mRecyclerView;
private List <ItemsListModel> myItems;
private ItemsListModel mItem;
private Button btnSelect;
public SelectItemFragment newInstance(String param1) {
SelectItemFragment fragment = new SelectItemFragment();
args = new Bundle();
args.putString("valuesArray", param1);
String mParam1 = param1;
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
mParam1 = bundle.toString();
String name = this.getArguments().getString("valuesArray").toString();
String newParam1 = name;
View view = inflater.inflate(R.layout.fragment_select_item, container, false);
btnSelect = (Button)view.findViewById(R.id.btnSelectItem);
myItems = DatabaseMgr.getItemsList(newParam1);
if (myItems.size() > 0) {
myItems.get(0).setSelected(true);
mItem = new ItemsListModel(myItems.get(0));
}
mRecyclerView = (RecyclerView) view.findViewById(R.id.lRecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerAdapter = new RecyclerAdapter(myItems);
mRecyclerView.setAdapter(mRecyclerAdapter);
return (view);
}
private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerHolder> {
private RecyclerViewClickListener mListener;
private List <ItemsListModel> items;
RecyclerAdapter(RecyclerViewClickListener listener){
mListener=listener;
}
public RecyclerAdapter(List <ItemsListModel> itemList) {
items = itemList;
}
@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_select_item, parent, false);
return new RecyclerHolder(view);
}
public void onBindViewHolder(RecyclerHolder recyclerholder, int position) {
ItemsListModel item = items.get(position);
if (item.isSelected()) {
recyclerholder.itemView.setBackgroundColor(getResources().getColor(R.color.colorYellow));
} else {
recyclerholder.itemView.setBackgroundColor(getResources().getColor(R.color.colorWhite));
}
recyclerholder.bindRecyclerData(item);
}
@Override
public int getItemCount() {
return items.size();
}
}
public class RecyclerHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mlItem;
private TextView mLVL;
private TextView mLVLd;
private TextView mDesc;
private ItemsListModel mItems;
public RecyclerHolder(View itemView) {
super(itemView);
mlItem = (TextView) itemView.findViewById(R.id.text_view_item);
mLVL = (TextView) itemView.findViewById(R.id.text_view_LVL);
mLVLd = (TextView) itemView.findViewById(R.id.text_view_LVLd);
mDesc = (TextView) itemView.findViewById(R.id.text_view_desc);
}
public void bindRecyclerData(ItemsListModel newItem) {
mItems = newItem;
mlItem.setText(mItems.getiItem());
mLVL.setText(mItems.getiLVL());
mLVLd.setText(mItems.getiLVLd());
mDesc.setText(mItems.getiDesc());
}
@Override
public void onClick(View view) {
for (int i = 0; i < myItems.size(); i++) {
if (myItems.get(i).equals(mItem)) {
myItems.get(i).setSelected(true);
mItem = new ItemsListModel(myItems.get(i));
} else {
myItems.get(i).setSelected(false);
}
}
mRecyclerAdapter.notifyDataSetChanged();
}
public void onbtnClick(View view){
}
}
public interface RecyclerViewClickListener {
void onClick(View view, int position);
}
}
片段的XML是。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.procatdt.stockright.activities.SelectItemFragment">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- <FrameLayout
android:id="@+id/newFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">-->
<TextView
android:id="@+id/text_view_item"
android:layout_width="65dp"
android:layout_height="35dp"
android:gravity="start|right"
android:text="Item" />
<TextView
android:id="@+id/text_view_LVL"
android:layout_width="40dp"
android:layout_height="35dp"
android:layout_toRightOf="@+id/text_view_item"
android:text="LVL" />
<TextView
android:id="@+id/text_view_LVLd"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_toRightOf="@+id/text_view_LVL"
android:text="LvlDesc" />
<TextView
android:id="@+id/text_view_desc"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_toRightOf="@+id/text_view_LVLd"
android:text="Description" />
<android.support.v7.widget.RecyclerView
android:id="@+id/lRecyclerView"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingTop="8dp"
android:scrollbars="horizontal" />
<Button
android:id="@+id/btnSelectItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="220dp"
android:onClick="onbtnClick"
android:text="Select Item" />
<!--</FrameLayout>-->
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorBackgroundGray"
android:layout_below="@id/layout"
style="?android:listSeparatorTextViewStyle"
/>
</FrameLayout>
当我点击recyclerview中的任何项目时,没有任何反应。我希望我点击的项目突出显示,让我知道选择了recyclerview中的哪一行。我一直没有关于活动中的recyclerviews的问题,只有碎片问题。
任何指针都会非常感激。 汤姆
答案 0 :(得分:2)
我想你忘了itemView.setOnClickListener(this);
试试这个:
public RecyclerHolder(View itemView) {
super(itemView);
mlItem = (TextView) itemView.findViewById(R.id.text_view_item);
mLVL = (TextView) itemView.findViewById(R.id.text_view_LVL);
mLVLd = (TextView) itemView.findViewById(R.id.text_view_LVLd);
mDesc = (TextView) itemView.findViewById(R.id.text_view_desc);
itemView.setOnClickListener(this);
}