标签: android-recyclerview

在recylerview中的NPE oncreateviewholder android

这应该是一项简单的工作.我刚从api调用中获取数据,并希望在recyclerview中填充它.我做过这样的事情就像我猜的每个人一样.但是在这里,当视图项布局在适配器类中膨胀时,我得到空指针异常.我不知道如何解决它.

我的适配器类是

public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {

private List<RecipeByIngredients> recipeByIngredients;
private Context mContext;

public RecipeAdapter(List<RecipeByIngredients> recipeByIngredients, Context mContext) {
    this.recipeByIngredients = recipeByIngredients;
    this.mContext = mContext;
}


@Override
public RecipeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
    final View itemView = mInflater.inflate(R.layout.recipe_item_layout, parent, false);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    RecipeByIngredients recipeByIngredient = recipeByIngredients.get(position);

    holder.tvUsedIngredients.setText(recipeByIngredient.getUsedIngredientCount() + " " + "ingredients used");
    holder.tvMissedIngredients.setText(recipeByIngredient.getMissedIngredientCount() + " " + "missed ingredients");

    Glide.with(mContext)
            .load(recipeByIngredient.getImage())
            .placeholder(R.drawable.recipe_placeholder) …
Run Code Online (Sandbox Code Playgroud)

java android nullpointerexception recycler-adapter android-recyclerview

0
推荐指数
1
解决办法
436
查看次数

RecyclerView显示相同的项目

我正在尝试实现RecyclerView,显示来自JSONArray的数据.解析工作完美,但RecyclerView在每个项目中显示必须仅在最后一项中的信息.屏幕截图清楚地表明了这一点.

这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    new parseTask().execute();
}

private class parseTask extends AsyncTask<Void, Void, String> {

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String resultJson = "";
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this, R.style.MyDialogStyle);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            URL url = new URL("http://songo.eu.pn/db_GetFromSongs.php");
            urlConnection = …
Run Code Online (Sandbox Code Playgroud)

java android recycler-adapter android-recyclerview

0
推荐指数
1
解决办法
1715
查看次数

快速浏览时,Recycler View显示错误的数据。

我有一个RecycleView地方,我对细胞有三种不同的看法。RView首次加载时,数据可以完美显示。当我开始滚动时,它会更改视图颜色。尝试了不同的方法,但无法解决。 原始观点错误的看法

public class TimelineRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int DATE_LAYOUT = 1;
private static final int EVEN_LAYOUT = 2;
private static final int ODD_LAYOUT = 3;
private List<TimeLineResponseDO.content> contents = new ArrayList<>();
Context context;
LayoutInflater layoutInflater;
private boolean isVisible = false;
private ConnectionDetector connectionDetector;
private ProgressDialog progressDialog;
private Fragment fragment1;
DateFormat inputFormat;
DateFormat outputFormat;
int counterOnCreate = 0;
int counterOnBind = 0;
public interface OpenSubmissionDetails{
    void openSubmissionDetails(String submissionId);
}
public interface …
Run Code Online (Sandbox Code Playgroud)

android recycler-adapter android-recyclerview recyclerview-layout

0
推荐指数
1
解决办法
2055
查看次数

ScrollView慢速滚动且项目消失

我正在尝试添加ScrollView来保存标头和RecyclerView。

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/profile"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="?attr/colorPrimary">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/imageView"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />


    </RelativeLayout>


    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:scrollbars="none"
        android:layout_alignParentLeft="true" />


    </RelativeLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

但是我有两个问题尝试这样做:

  • 相对布局ID /配置文件正在消失。当我打开活动时,它会显示1秒钟,然后消失。

  • RecyclerView滚动太慢。

有任何想法吗?

android android-layout android-xml android-scrollview android-recyclerview

0
推荐指数
1
解决办法
885
查看次数

如何以编程方式删除RecyclerView中的行

嘿家伙我有一个recyclerview适配器,它填充了cardview上的一些数据列表.我在卡片布局的右下角创建了一个三点菜单,它实际上会从recyclerview中删除卡片行,并且还会从存储行数据的sqlite数据库中删除该项目.问题是我无法弄清楚如何将实现删除的方法集成到recyclerview ViewHolder中.这是源代码

public class BeneficiaryRecyclerAdapter extends RecyclerView.Adapter<BeneficiaryRecyclerAdapter.BeneficiaryViewHolder> {

private List<Beneficiary> listBeneficiary;
public ImageView overflow;
private Context mContext;

public BeneficiaryRecyclerAdapter(){

}


public BeneficiaryRecyclerAdapter(List<Beneficiary> listBeneficiary, Context mContext) {
    this.listBeneficiary = listBeneficiary;
    this.mContext = mContext;

}


@Override
public BeneficiaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // inflating recycler item view
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_beneficiary_recycler, parent, false);

    return new BeneficiaryViewHolder(itemView);
}

@Override
public void onBindViewHolder(final BeneficiaryViewHolder holder, int position) {
    holder.textViewName.setText(listBeneficiary.get(position).getName());
    holder.textViewEmail.setText(listBeneficiary.get(position).getEmail());
    holder.textViewAddress.setText(listBeneficiary.get(position).getAddress());
    holder.textViewCountry.setText(listBeneficiary.get(position).getAddress());

    holder.overflow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPopupMenu(holder.overflow); …
Run Code Online (Sandbox Code Playgroud)

sqlite android-recyclerview cardview

0
推荐指数
1
解决办法
1489
查看次数

分隔线未显示在回收器视图项中

在我的应用程序中,我使用包含许多项目的回收站视图,我想在项目之间显示一个无线分隔符(分隔线),但它不起作用.我已经尝试创建一个可绘制的形状,但是如果在dividerItemDecoration中添加drawable,不会在回收器视图项之间显示空格或线.我也尝试过创建自定义的DividerItemDecoration类,但对我来说没什么用. 注意:目前在我的drawable形状设置为矩形我也试过线.如何实现.任何帮助将不胜感激.这是我的代码.

绘制对象:(customdrawableshape.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="3dp"
        android:height="2dp"
        android:color="#000000"
        android:dashGap="10dp"
        android:dashWidth="5dp" />

</shape>
Run Code Online (Sandbox Code Playgroud)

回收站视图项的自定义行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginBottom="2dp">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

以及我将项目装饰设置为recyclerview的代码部分

DividerItemDecoration dividerItemDecoration;
recyclerview.setLayoutManager(linearlayoutmanager);
dividerItemDecoration = new DividerItemDecoration(recyclerview.getContext(),
        linearlayoutmanager.getOrientation());
dividerItemDecoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.customdrawableshape));
recyclerview.addItemDecoration(dividerItemDecoration);
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview

0
推荐指数
1
解决办法
5660
查看次数

按钮不显示在Android Studio中的RecycleView下方

设计布局RecycleView用于显示图像.下面添加了一个按钮RecycleView以保存列表中的所选项目.在设计视图中和运行应用程序时,按钮不可见.无法追踪问题所在.代码是:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="com.example.user.recycleview.imagesview">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:theme="@style/AppTheme.AppBarLayout">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/titlebar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<view
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="android.support.v7.widget.RecyclerView"
    android:id="@+id/recycler_view" />

<Button
    android:id="@+id/button7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/savebutton"
    android:text="@string/save"
    android:textColor="@color/white"
    android:textSize="12pt" />


<!--
Deleted fab element
-->

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview

0
推荐指数
1
解决办法
573
查看次数

如何将文本视图数据从回收站视图转移到另一个活动?

我想将文本视图数据从回收站视图带到另一个活动.我想将workshop_name TextView带到Otheractivity.

这是我的代码.

MechSearchActivity.java

public class MechSearchActivity extends AppCompatActivity {
List<SearchResponse.WorkshopDataBean> workshopList;
RecyclerView recyclerView;
  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_mec);

recyclerView = (RecyclerView) findViewById(R.id.rv_workshoplist);

callSearchApi();

onItemClick();
 }

   public void callSearchApi() {


    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    Call<SearchResponse> call = apiService.getSearchData( "workshop_name","image", "street",  "locality", "city"); // single search for all the values
    call.enqueue(new Callback<SearchResponse>() {

        @Override
        public void onResponse(Call<SearchResponse> call, retrofit2.Response<SearchResponse> response) {
            SearchResponse searchResponse = response.body();
           workshopList = searchResponse.getWorkshopData();
            MechanicRecyclerAdapter adapter = new MechanicRecyclerAdapter(MechSearchActivity.this, workshopList);
            recyclerView.setAdapter(adapter);
            LinearLayoutManager mGridLayoutManager = …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview

0
推荐指数
1
解决办法
161
查看次数

在clickview上点击几秒钟后,运行一个小吃吧

我有一个循环视图,当在recycleview上点击几秒钟时,运行一个快餐栏.怎么做?我很困惑寻找在谷歌搜索的关键字.我可以这样:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Glide.with(context)
            .load("http://bls.hol.es/" + list_data.get(position).get("poto"))
            .crossFade()
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.imghape);
    final String namamu=list_data.get(position).get("nama");
    holder.txthape.setText(namamu);
    holder.txtalamat.setText(list_data.get(position).get("alamat"));
    // Set onclicklistener pada view tvTitle (TextView)
    holder.txthape.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Clicked element "+namamu, Snackbar.LENGTH_LONG).show();
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

坦克..

android onclicklistener android-recyclerview

0
推荐指数
1
解决办法
366
查看次数

RecyclerView内容不使用片段父级的全宽度

所以我有一个片段内的recyclelerview,当内容加载到recyclerview时,它不占用屏幕的整个宽度.奇怪的是,我将这个代码用于另一个项目实际上是相同的并且没有问题.

片段布局是这样的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/windowBackground"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.dishesteam.dishes.activities.HomeActivity">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/coordinator">

        <android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swipeContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="4dp"
            android:paddingBottom="4dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:id="@+id/latest_recycler">

            </android.support.v7.widget.RecyclerView>

        </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/add_dish_fab"
        app:srcCompat="@drawable/ic_add_black_24dp"
        app:fabSize="normal"
        app:backgroundTint="@color/colorAccent"
        android:clickable="true"
        android:layout_margin="16dp"
        android:layout_gravity="bottom|end"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

</android.support.design.widget.CoordinatorLayout>

<ImageView
    android:id="@+id/trending_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_trending_up_black_24dp"
    android:layout_centerInParent="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:text="Nothing here at the moment"
    android:textSize="16sp"
    android:id="@+id/empty_text"
    android:layout_below="@+id/trending_img"
    android:layout_centerHorizontal="true"
    android:textColor="@color/ColorDarkGrey" />
Run Code Online (Sandbox Code Playgroud)

recyceler的各个项目的布局是

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/textColorPrimary"
app:cardCornerRadius="4dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp">

<LinearLayout …
Run Code Online (Sandbox Code Playgroud)

xml android android-layout android-fragments android-recyclerview

0
推荐指数
1
解决办法
592
查看次数