如何使用RecyclerView构建水平ListView?


此示例演示了如何使用RecyclerView构建水平ListView。

步骤1 - 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建一个新项目。

步骤2 - 将以下代码添加到res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.design.widget.CoordinatorLayout android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto">
   <android.support.design.widget.AppBarLayout
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content">
      <android.support.v7.widget.Toolbar
         android:id = "@+id/appbarlayout_tool_bar"
         android:background = "@color/colorPrimary"
         android:layout_width = "match_parent"
         android:layout_height = "?attr/actionBarSize"
         app:layout_scrollFlags = "scroll|snap|enterAlways"
         app:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar"
         app:popupTheme = "@style/ThemeOverlay.AppCompat.Light" />
   </android.support.design.widget.AppBarLayout>
   <android.support.v7.widget.RecyclerView
      android:id = "@+id/recycler_view"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      app:layout_behavior = "@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

在上面的代码中,我们使用了RecyclerView。

步骤3 - 将以下代码添加到src/MainActivity.java

package com.example.myapplication;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
   TextView text;
   ArrayList<String> list = new ArrayList<>();
   private RecyclerView recyclerView;
   private customAdapter mAdapter;
   private onClickInterface onclickInterface;
   @TargetApi(Build.VERSION_CODES.LOLLIPOP)
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.appbarlayout_tool_bar);
      toolbar.setTitle("This is toolbar.");
      setSupportActionBar(toolbar);
      onclickInterface = new onClickInterface() {
         @Override
         public void setClick(int abc) {
            list.remove(abc);
            Toast.makeText(MainActivity.this,"Position is"+abc,Toast.LENGTH_LONG).show();
            mAdapter.notifyDataSetChanged();
         }
      };
      recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
      RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL, false);
      recyclerView.setLayoutManager(mLayoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
      mAdapter = new customAdapter(this, list, onclickInterface);
      recyclerView.setAdapter(mAdapter);
      DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
    DividerItemDecoration.HORIZONTAL);
      recyclerView.addItemDecoration(dividerItemDecoration);
      list.add("sairamm");
      list.add("Krishna");
      list.add("prasad");
      list.add("sairamm");
      list.add("Krishna");
      list.add("prasad");
      list.add("sairamm");
      list.add("Krishna");
      list.add("prasad");
      list.add("sairamm");
      list.add("Krishna");
      list.add("prasad");
      list.add("Krishna");
      list.add("prasad");
      list.add("sairamm");
      list.add("Krishna");
      list.add("prasad");
      list.add("sairamm");
      list.add("Krishna");
      list.add("prasad");
   }
}

步骤4 - 将以下代码添加到src/customAdapter.java

package com.example.myapplication;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;

public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder> {
   Context context;
   ArrayList<String> list;
   onClickInterface onClickInterface;
   public class MyViewHolder extends RecyclerView.ViewHolder {
      public TextView title;
      public MyViewHolder(View view) {
         super(view);
         title = (TextView) view.findViewById(R.id.title);
      }
   }
   public customAdapter(Context context, ArrayList<String> list, onClickInterface onClickInterface) {
      this.context = context;
      this.list = list;
      this.onClickInterface = onClickInterface;
   }
   @NonNull
   @Override
   public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false);
      return new MyViewHolder(itemView);
   }
   @Override
   public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
      myViewHolder.title.setText(list.get(i));
      myViewHolder.title.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            onClickInterface.setClick(i);
         }
      });
   }
   @Override
      public int getItemCount() {
         return list.size();
      }
}

步骤5 - 将以下代码添加到res/layout/list_row.xml。

<?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"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content"
   android:padding = "10dp"
   tools:context = ".MainActivity">
   <LinearLayout
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:gravity = "center"
      android:orientation = "vertical">
      <ImageView
         android:id = "@+id/imageView2"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:src = "@drawable/logo" />
      <TextView
         android:id = "@+id/title"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:gravity = "center"
         android:textSize = "30sp" />
      <TextView
         android:id = "@+id/textview2"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:gravity = "center"
         android:text = "Sairamkrishan"
         android:textSize = "30sp" />
   </LinearLayout>
</android.support.v7.widget.CardView>

步骤6 - 将以下代码添加到src/onClickInterface。

package com.example.myapplication;
public interface onClickInterface {
   void setClick(int abc);
}

步骤7 - 将以下代码添加到res/drawable/divider.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android" android:shape = "rectangle">
   <solid android:color = "@color/colorPrimary"/>
   <size android:height = "2dp"/>
</shape>

让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开您的项目中的一个活动文件,然后单击运行 工具栏中的图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 –


更新于:2019年7月30日

147 次浏览

启动您的职业生涯

通过完成课程获得认证

开始
广告