簡體   English   中英

除非單擊Edittext或專注於加載,否則不會顯示RecyclerView

[英]RecyclerView is not showing unless Edittext is clicked or focused on load

我嘗試了此處提供的解決方案:除非單擊Edittext,否則RecyclerView不會顯示,但仍不會顯示。 數據是從某些URL的CSV文件中調用的。

MainActivity代碼:

    //getting the recyclerview from xml
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    //getting CSV data from URL
    DownloadFilesTask downloadFilesTask = new DownloadFilesTask();
    downloadFilesTask.execute();

    //initializing the productlist
    productList = new ArrayList<>();

    //creating recyclerview adapter
    ProductAdapter adapter = new ProductAdapter(this, productList);
    adapter.notifyItemInserted(productList.size());
    adapter.notifyDataSetChanged();

    //setting adapter to recyclerview
    recyclerView.setAdapter(adapter);

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity">

<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="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true">

<Spinner
    android:id="@+id/filter_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:entries="@array/filter_array"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:ems="10"
    android:inputType="text"
    android:text="Search"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/filter_spinner" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    app:layout_constraintTop_toBottomOf="@+id/editText"/>

這是從URL獲取CSV數據的代碼:

private class DownloadFilesTask extends AsyncTask<URL, Void, List<String[]>> {
    protected List<String[]> doInBackground(URL... urls) {
        return downloadRemoteTextFileContent();
    }
    protected void onPostExecute(List<String[]> result) {
        if(result != null){
            printCVSContent(result);
        }
    }
}

private void printCVSContent(List<String[]> result){
    String cvsColumn = "";
    String distanceKM;
    for (int i = 0; i < result.size(); i++){
        String [] rows = result.get(i);
        //cvsColumn += rows[0] + " " + rows[1] + " " + rows[2] + "\n";

        //adding some items to our list
        distanceKM = getDistance(latitude, longitude, Double.parseDouble(rows[2]),Double.parseDouble(rows[3]));
        productList.add(
                new Product(
                        rows[0],
                        rows[1].replace(';', ','),
                        rows[2],
                        rows[3],
                        rows[4],
                        rows[5],
                        rows[6],
                        distanceKM,
                        rows[7].replace(';', '\n')));

    }

我認為不需要在產品適配器上顯示其他代碼。 我使用大多數代碼的源代碼在這里: https : //www.simplifiedcoding.net/android-recyclerview-cardview-tutorial/#RecyclerView-Item-Layout-using-CardView我只是與代碼結合使用。

由於DownloadFilesTask是AsyncTask,因此您無法確定在此行之前添加產品的過程是否完成:

ProductAdapter adapter = new ProductAdapter(this, productList);

您可以在調用AsyncTask.execute()之前在主線程上初始化productList和適配器。 然后在AsyncTask完成后調用notifyDataSetChanged

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM