読者です 読者をやめる 読者になる 読者になる

【Android】横リストを作成する(RecyclerView)

 

 

f:id:stanleyblenz:20170115215219p:plain

 

 

横リストの作成方法です。

RecyclerView を使用します。

(横・縦リストが作成できます)

ListViewとほとんど同じ感じでできます。

 

build.gradle

dependencies {
  compile 'com.android.support:recyclerview-v7:23.0.1'
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  android:id="@+id/activity_main"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="16dp">
  <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview1"
    android:layout_marginTop="10dp"
    android:layout_alignParentLeft="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

 

recycler_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
    android:id="@+id/recyclerAdapterTextview"
    android:visibility="gone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

 

MainActivity.java

private RecyclerView recyclerView = null;
private RecyclerAdapter adapter = null;

private void recyclerviewCreate() {
  recyclerView = (RecyclerView)findViewById(R.id.recyclerview1);
  LinearLayoutManager manager = new LinearLayoutManager(this);
  // manager.setOrientation(LinearLayoutManager.VERTICAL);//縦リスト
  manager.setOrientation(LinearLayoutManager.HORIZONTAL);//横リスト
  recyclerView.setLayoutManager(manager);

  //データ作成
  ArrayList<ScreenShot> list = getExternalScreenShot();
  if (list == null || list.size() == 0) {
    return;
  }
  adapter = new RecyclerAdapter(this, list, this);
  recyclerView.setAdapter(adapter);
}

//コンテンツプロバイダに保存した画像(スクリーンショット)を取得

private ArrayList<ScreenShot> getExternalScreenShot() {

  ArrayList<ScreenShot> list = new ArrayList<ScreenShot>();
  String projection = new String {
    MediaStore.Images.Media.DATA};
  Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  Cursor cur = getContentResolver().query(uri,
                     projection,
                        null,
                     null,
                     null);
  if (cur == null) {
    return list;
  }
  if (cur.moveToFirst()) {
    do {
      String filepath = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));
      Bitmap bitmap = BitmapFactory.decodeFile(filepath);
      ScreenShot sh = new ScreenShot(bitmap, filepath);
      list.add(sh);
    } while (cur.moveToNext());
  }
  return list;

}

 

RecyclerAdapter.java

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

  private LayoutInflater inflater;
  private ArrayList<ScreenShot> list;
  private RecyclerListener listener;
  public RecyclerAdapter(Context context, ArrayList<ScreenShot> list, RecyclerListener listener) {
    this.inflater = LayoutInflater.from(context);
    this.list = list;
    this.listener = listener;
  }
  @Override
  public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    return new ViewHolder(inflater.inflate(R.layout.recycler_adapter, viewGroup, false));
}
  @Override
  public void onBindViewHolder(ViewHolder viewHolder, final int i) {
    if (list != null && list.size() > 0) {
      ScreenShot item = list.get(i);
      viewHolder.imageView.setImageBitmap(item.screenshot);
      viewHolder.textView.setText(item.filepath);
    }
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      //クリック処理 コールバックを実装する(MainActivityで受ける)
      }
    });
    viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
   //ロングクリック処理 コールバックを実装する(MainActivityで受ける)
        return true;
      }
    });
  }
  //ViewHolder
  class ViewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;
    TextView textView;
    public ViewHolder(View itemView) {
      super(itemView);
      imageView = (ImageView)itemView.findViewById(R.id.recyclerAdapterImage);
      textView = (TextView)itemView.findViewById(R.id.recyclerAdapterTextview);
    }
  }
}

 

ScreenShot.java

public class ScreenShot implements Parcelable {

  Bitmap screenshot;
  String filepath;

  public ScreenShot(Bitmap screenshot, String filepath) {
    this.screenshot = screenshot;
    this.filepath = filepath;
  }
  protected ScreenShot(Parcel in) {
    this.screenshot = in.readParcelable(Bitmap.class.getClassLoader());
    this.filepath = in.readString();
  }
  @Override
  public int describeContents() {
    return 0;
  }
  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(screenshot, flags);
    dest.writeString(filepath);
  }
  public static final Parcelable.Creator CREATOR
      = new Parcelable.Creator() {
    public ScreenShot createFromParcel(Parcel in) {
      return new ScreenShot(in);
    }
    public ScreenShot[] newArray(int size) {
      return new ScreenShot[size];
    }
  };
}