我正在使用recycler标头开发一个应用程序。而我的回收商有头。源代码回收标头显示在https://github.com/blipinsk/RecyclerViewHeader中。
我想在回收站标题中激活点击侦听器上的设置。当我点击回收器标题时,它会显示一个烤制的消息。我的代码如下。
我在片段中使用。
HomeFragment.java
public class HomeFragment extends android.support.v4.app.Fragment {
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
RecyclerViewHeader recyclerHeader;
String GET_JSON_DATA_HTTP_URL;
String JSON_IMAGE_TITLE_NAME = "image_title";
String JSON_IMAGE_URL = "image_url";
String JSON_IMAGE_ID = "id";
String JSON_MRP = "mrp";
String JSON_RATE = "rate";
String JSON_DISCOUNT = "discount";
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home");
GET_JSON_DATA_HTTP_URL = Util.URL_SERVER + "categories.php";
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview3);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(recyclerViewlayoutManager);
recyclerHeader = (RecyclerViewHeader) rootView.findViewById(R.id.header);
recyclerHeader.attachTo(recyclerView);
recyclerHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG", "clicked");
}
});
JSON_DATA_WEB_CALL();
// Inflate the layout for this fragment
return rootView;
}
public void JSON_DATA_WEB_CALL(){
final ProgressDialog progressDialog = new ProgressDialog(getContext(), R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Loading");
progressDialog.show();
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressDialog.dismiss();
Log.d("LOGTAG", "Response :"+response);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImageTitleName(json.getString(JSON_IMAGE_TITLE_NAME));
GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));
GetDataAdapter2.setImageId(json.getString(JSON_IMAGE_ID));
GetDataAdapter2.setMrp(json.getString(JSON_MRP));
GetDataAdapter2.setDiscount(json.getString(JSON_DISCOUNT));
//Log.d("LOGTAG", "JSON_MRP : "+json.getString(JSON_MRP)+"JSON_RATE : "+json.getString(JSON_RATE)+"JSON_DISCOUNT :"+json.getString(JSON_DISCOUNT));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, getContext());
recyclerView.setAdapter(recyclerViewadapter);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
static String categoryId;
Context context;
List<GetDataAdapter> getDataAdapter;
ImageLoader imageLoader1;
private final View.OnClickListener mOnClickListener = new MyOnClickListener();
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.subcategory_items, parent, false);
v.setOnClickListener(mOnClickListener);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
Viewholder.Mrp.setText(getDataAdapter1.getMrp());
Viewholder.Discount.setText(getDataAdapter1.getDiscount());
categoryId = getDataAdapter1.getImageTitleName();
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ImageTitleNameView;
public TextView imageid;
public NetworkImageView networkImageView ;
public CardView cardView ;
public TextView Mrp;
public TextView Rate;
public TextView Discount;
public ViewHolder(final View itemView) {
super(itemView);
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item) ;
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);
cardView = (CardView) itemView.findViewById(R.id.cardview1);
Mrp = (TextView) itemView.findViewById(R.id.textViewMrp) ;
Rate = (TextView) itemView.findViewById(R.id.textViewRate) ;
Discount = (TextView) itemView.findViewById(R.id.textViewDiscount) ;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
Log.d("LOGTAG", "position : "+pos);
Log.d("LOGTAG", "position : "+getDataAdapter.get(pos).getImageId());
ProductFragment fragobj=new ProductFragment();
Bundle bundle=new Bundle();
bundle.putString("subcategory_Product_ID", getDataAdapter.get(pos).getImageId());
fragobj.setArguments(bundle);
switchFragment(R.id.container_body, fragobj); //replace container ID
}
});
}
}
public void switchFragment(int id, Fragment fragment) {
if (context == null)
return;
//MainActivity is your activity where FriendsFragment is called
if (context instanceof HomeActivity) {
HomeActivity homeActivity = (HomeActivity) context;
homeActivity.loadFragment(id, fragment);
}
}
private class MyOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
GetDataAdapter getDataAdapter = new GetDataAdapter();
}
}
}
fragment_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/title_padding"
android:paddingRight="@dimen/title_padding">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"/>
<com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:layout_gravity="center_horizontal|top"
android:layout_margin="@dimen/recycler_divider_width"
android:background="@drawable/rounded_background_header">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="?selectableItemBackgroundBorderless"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:text="@string/header"
android:textColor="@color/clouds"
android:textSize="20sp"/>
</com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader>
</FrameLayout>
layout_item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/recycler_divider_width"
android:background="@drawable/round_background_green">
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_gravity="center"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:text="@string/item"
android:textSize="16sp"
android:textColor="@color/clouds" />
</FrameLayout>
答案 0 :(得分:0)
您在OnClickListener
上设置了RecyclerViewHeader
,我相信您要将其设置在RecyclerViewHeader
内的按钮上。
android:id
添加到Button
RecyclerViewHeader
醇>
<Button
android:id="@+id/button_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="?selectableItemBackgroundBorderless"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:text="@string/header"
android:textColor="@color/clouds"
android:textSize="20sp"/>
Button
HomeFragment
醇>
recyclerHeader = (RecyclerViewHeader) rootView.findViewById(R.id.header);
recyclerHeader.attachTo(recyclerView);
Button headerButton = (Button) recyclerHeader.findViewById(R.id.button_header);
OnClickListener
而不是headerButton
RecyclerViewHeader
醇>
headerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG", "clicked");
}
});
答案 1 :(得分:0)
我认为onClickListener
不适用于RecyclerViewHeader
的原因是因为在RecyclerViewHeader
内你有Button
。
此按钮将显示在RecyclerViewHeader
上方,宽度/高度匹配RecyclerViewHeader
因此,您永远无法点击RecyclerViewHeader
,只需点击Button
如果您仍想在onClickListener
上处理RecyclerViewHeader
,则需要将Button
的尺寸设置得更小,然后点击RecyclerViewHeader