android-recyclerview

Android: should I maintain a reference to the activity inside a recyclerview or is there another way?

烈酒焚心 提交于 2019-12-06 07:19:54
问题 I need to use context methods within the onBindViewHolder (a standard example might be something as common as getString or getColor). Until now I've passed the context to the constructor for the recyclerview and maintained a reference in a variable, however this seems to me to be bad practice. Is there a way of getting context dynamically from inside a recyclerview without storing it as a variable? public SomeRecyclerViewClass(Activity activity) { this.parentActivity = activity; } 回答1: I

RecyclerView - findViewHolderForPosition always returns null when onBindViewHolder is not called

筅森魡賤 提交于 2019-12-06 06:48:22
问题 I've been trying to use the new RecyclerView widget for a couple of days now and thought I've managed to create a Horizontal and Grid layout and also overcome the fact that it lacks of a straightforward way of implementing a OnClickListener (I used GreenRobot's EventBus for this) , I'm still having a hard time selecting an item and preserving that state (style) throughout the recycling process. This is the code I'm using for my adapter public class ArticuloItemAdapter extends RecyclerView

Create RecyclerView item layout based on content of data

荒凉一梦 提交于 2019-12-06 06:38:02
I'm creating a recycler view of data objects, each of these object will vary, eg. Obj 1 - String Heading - String Desc - Image Obj 2 - String Desc Obj 3 - Image - String Link Obj 4 - String Desc - Video etc So I need to create my item layout dynamically to suit each data object. I don't want to create an item layout with all the possible views and components and show and hide as I feel it's bad practice. So my issue is I need to access an object from the list using the position in the onBindViewHolder in order to construct my layout in the ViewHolder class. I tried using a method which I

maxHeight does not work on RecyclerView

戏子无情 提交于 2019-12-06 06:35:00
问题 I've a DialogFragment . Layout: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="0dp" android:layout_height="wrap_content" android:maxHeight="280dp" app:layout_constraintEnd_toEndOf=

Switch between RecyclerView layouts when click on AlertDialog item list

落爺英雄遲暮 提交于 2019-12-06 06:33:04
问题 I have three different layouts cardsListLayout , titleLayout , cardMagazineLayout and there may be more in the future, which used as a views on onCreateViewHolder method. I want to switch between views in onCreateViewHolder method so when user choose from AlertDialog list this onOptionsItemSelected in MainActivity Where there is menu Item "change the layout" so that when the user presses it it appears AlertDialog list @Override public boolean onOptionsItemSelected(MenuItem item) { if (item

Android - why lost values EditText when scrolled RecyclerView?

走远了吗. 提交于 2019-12-06 06:31:21
I am using from bellow code for converting numbers to currency format when text changes but when I scrolled the recyclerView I lost true data. What can I do : public class ManagePriceProductsAdapter extends RecyclerView.Adapter<ManagePriceProductsAdapter.ViewHolder> { private List<ManagePriceProductsModel.DataValue> managePriceProductsModelList; private Context context; private getListDiscountInterface getListDiscountInterface; private int vendorId = -1; public ManagePriceProductsAdapter(Context context, List<ManagePriceProductsModel.DataValue> managePriceProductsModelList,

Kotlin Android Fragment recyclerView and context issue

社会主义新天地 提交于 2019-12-06 06:26:18
问题 I would like to create an recyclerView in a fragment, but it shows an error " java.lang.IllegalStateException: recylerView_Main must not be null at com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)" Question 1) Please kindly help to solve this issue. Question 2) I created an recyclerView only in a empty project without any fragment, it is working properly. But the same code is no working in Fragment, it shows error so I change "recylerView_Main.layoutManager =

Starting DialogFragment from a class extending RecyclerView.ViewHolder

╄→尐↘猪︶ㄣ 提交于 2019-12-06 06:00:43
问题 I tried as below at onClick() method of recyelerview.viewholder class. SampleDialogFragment used in the sample extends DialogFragment. @Override public void onClick(View v) { SampleDialogFragment df= new SampleDialogFragment(); df.show(v.getContext().getSupportFragmentManager(), "Dialog"); } I'm facing problem at v.getContext().getSupportFragmentManager() . I can't call getSupportFragmentManager(). I also tried as below . @Override public void onClick(View v) { SampleDialogFragment df= new

RecyclerView listen to item added

旧街凉风 提交于 2019-12-06 05:57:51
I have a RecyclerView and I use the following line to add new items: recyclerAdapter.notifyItemInserted(newItemIndex); Is there a listener I can use to know when the RecyclerView has finished adding the item? I would try creating a subclass of the DefaultItemAnimator overriding the onAddFinished method. public class MyDefaultItemAnimator extends DefaultItemAnimator { @Override public void onAddFinished(RecyclerView.ViewHolder item) { super.onAddFinished(item); String text = "Add element: " + item.getPosition(); Toast.makeText(MyActivity.this, text, Toast.LENGTH_SHORT).show(); } } And then:

RecyclerView and ViewPager with collapsing toolbar

一曲冷凌霜 提交于 2019-12-06 05:49:13
I am trying to create this kind of a layout where - A CollapsingToolbarLayout that has a ViewPager like google play app. Below that there is a Grid RecyclerView . And at the end, there is another ViewPager . The RecylerView scrolls normally until the end when CollapsingToolbarLayout expands. <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <android