Android RecyclerView无法正常工作

时间:2018-11-09 06:16:20

标签: android android-recyclerview

我是Android编程新手,现在仍然在学习。我现在遇到问题,这是我的RecyclerView无法正常工作。当我运行我的应用程序时,logcat上没有显示任何错误。问题是:

  • 有时它将加载所有项目,但是有时它将仅显示空白屏幕,因此我通常按返回按钮并再次输入活动,它将加载项目。
  • 有时它会加载项目,但我对recyclerview不能做任何事情。我无法单击或滚动RecyclerView
  • 有时它可以100%无误地工作。

我在CardView中使用RecyclerView,并且正在使用Retrofit库。

这是我的代码:

Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myappcompany.jason.financepharos">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Finance Demo Apps"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!-- Login Activity - Form 1 -->
    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Main Activity - Form 2 -->
    <activity android:name=".MainActivity"/>

    <!-- Master Project Activity - Form 3 -->
    <activity android:name=".MasterProjectActivity" />

    <!-- Project Activity (Create) - Form 3.1 -->
    <activity android:name=".ProjectActivity"
        android:noHistory="true"/>

    <!-- Project Activity (Read/View) - Form 3.2 -->
    <activity android:name=".ProjectActivityView"
        android:noHistory="true"/>

    <!-- Project Activity (Update and Delete) - Form 3.3 -->
    <activity android:name=".ProjectActivityUpdate"
        android:noHistory="true"/>
  </application>

  </manifest>

XML(RecyclerView)

<?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="match_parent"
android:orientation="vertical">

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

</LinearLayout>

XML(CardView)

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">

<android.support.v7.widget.CardView
    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:layout_margin="5dp"
    app:cardElevation="8dp"
    android:clickable="false">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:text="P. ID"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:text=":" />

            <TextView
                android:id="@+id/tvProjectID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:text="P. Name"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:text=":" />

            <TextView
                android:id="@+id/tvProjectName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:text="P. Application Name"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:text=":" />

            <TextView
                android:id="@+id/tvProjectApplicationName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:text="P. Aktif Y/N"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:text=":" />

            <TextView
                android:id="@+id/tvProjectAktifYN"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:text="P. Update ID"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:text=":" />

            <TextView
                android:id="@+id/tvProjectUpdateID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:text="P. Update Time"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:text=":" />

            <TextView
                android:id="@+id/tvProjectUpdateTime"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp" />
        </LinearLayout>

    </LinearLayout>

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

</LinearLayout>

我的适配器:

//Adapter Data Master Project

public class AdapterData extends RecyclerView.Adapter<AdapterData.HolderData> {

private List<DataModel> mList;
private Context ctx;

public AdapterData (Context ctx, List<DataModel> mList){
    this.ctx = ctx;
    this.mList = mList;
}

@NonNull
@Override
public HolderData onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layoutlistproject,viewGroup, false);
    HolderData holder = new HolderData(layout);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull HolderData holder, int i) {
    DataModel dm = mList.get(i);
    holder.MSTPRJ_ID.setText(dm.getMSTPRJ_ID());
    holder.MSTPRJ_Name.setText(dm.getMSTPRJ_Name());
    holder.MSTPRJ_ApplicationName.setText(dm.getMSTPRJ_ApplicationName());
    holder.MSTPRJ_AktifYN.setText(dm.getMSTPRJ_AktifYN());
    holder.MSTPRJ_UpdateID.setText(dm.getMSTPRJ_UpdateID());
    holder.MSTPRJ_UpdateTime.setText(dm.getMSTPRJ_UpdateTime());

    holder.dm = dm;
}

@Override
public int getItemCount() {
    return mList.size();
}

class HolderData extends RecyclerView.ViewHolder{
    TextView MSTPRJ_ID, MSTPRJ_Name, MSTPRJ_ApplicationName, MSTPRJ_AktifYN, MSTPRJ_UpdateID, MSTPRJ_UpdateTime;
    DataModel dm;
    public HolderData (View v){
        super(v);

        MSTPRJ_ID = (TextView) v.findViewById(R.id.tvProjectID);
        MSTPRJ_Name = (TextView) v.findViewById(R.id.tvProjectName);
        MSTPRJ_ApplicationName = (TextView) v.findViewById(R.id.tvProjectApplicationName);
        MSTPRJ_AktifYN = (TextView) v.findViewById(R.id.tvProjectAktifYN);
        MSTPRJ_UpdateID = (TextView) v.findViewById(R.id.tvProjectUpdateID);
        MSTPRJ_UpdateTime = (TextView) v.findViewById(R.id.tvProjectUpdateTime);

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent goInput = new Intent(ctx, ProjectActivityUpdate.class);
                goInput.putExtra("MSTPRJ_ID", dm.getMSTPRJ_ID());
                goInput.putExtra("MSTPRJ_Name", dm.getMSTPRJ_Name());
                goInput.putExtra("MSTPRJ_ApplicationName", dm.getMSTPRJ_ApplicationName());
                goInput.putExtra("MSTPRJ_AkitfYN", dm.getMSTPRJ_AktifYN());
                goInput.putExtra("MSTPRJ_UpdateID", dm.getMSTPRJ_UpdateID());
                goInput.putExtra("MSTPRJ_UpdateTime", dm.getMSTPRJ_UpdateTime());

                v.setOnClickListener(this);

                ctx.startActivity(goInput);
            }
        });
    }
}
}

我的活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_project_view);

    pd = new ProgressDialog(this);

    mRecyclerProject = (RecyclerView) findViewById(R.id.recyclerTemp);
    mRecyclerProject.setHasFixedSize(true);

    mManager = new 
    LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    mRecyclerProject.setLayoutManager(mManager);


    pd.setMessage("Loading...");
    pd.setCancelable(false);
    pd.show();

    Call<ResponsModel> call = RetroServer.retrofitRequest().readProject();
    call.enqueue(new Callback<ResponsModel>() {
        @Override
        public void onResponse(Call<ResponsModel> call, 
    Response<ResponsModel> response) {
            pd.hide();
            Log.d("RETRO", "Response : " + response.body().getKode());
            mItems = response.body().getResult();
            mAdapterProject = new AdapterData(ProjectActivityView.this, 
    mItems);
            mRecyclerProject.setAdapter(mAdapterProject);
            mAdapterProject.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<ResponsModel> call, Throwable t) {
            pd.hide();
            Log.d("RETRO", "Failed : Response Gagal");
        }
    });
}

@Override
public void onDestroy(){
    super.onDestroy();
    if(pd != null){
        pd.dismiss();
        pd = null;
    }
}
}

谢谢:)

0 个答案:

没有答案