Skip to content

Commit

Permalink
A LinearLayoutManager like LayoutManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
BCsl committed Dec 15, 2016
1 parent ea2a24e commit eb2abbb
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 97 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

Demonstrate how to custom `RecycleView#LayoutManager`

## LICENSE
## Iterative

### v1.0

## License

```
Copyright 2016 HelloCsl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,6 +23,10 @@ public class MainActivity extends AppCompatActivity {
RecyclerView mMainRecycle1;
@BindView(R.id.main_recycle2)
RecyclerView mMainRecycle2;
@BindView(R.id.main_tv_recycle_info_1)
TextView mMainTv1;
@BindView(R.id.main_tv_recycle_info_2)
TextView mMainTv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -35,12 +44,31 @@ private void initView() {
}
GalleryLayoutManager layoutManager1 = new GalleryLayoutManager(this, GalleryLayoutManager.HORIZONTAL);
mMainRecycle1.setLayoutManager(layoutManager1);
DemoAdapter demoAdapter1 = new DemoAdapter(title);
DemoAdapter demoAdapter1 = new DemoAdapter(title) {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mMainTv1.append("onCreateViewHolder\n");
return super.onCreateViewHolder(parent, viewType);
}
};
demoAdapter1.setOnItemClickListener(new DemoAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
}
});
mMainRecycle1.setAdapter(demoAdapter1);

GalleryLayoutManager layoutManager2 = new GalleryLayoutManager(this, GalleryLayoutManager.VERTICAL);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
mMainRecycle2.setLayoutManager(layoutManager2);
DemoAdapter demoAdapter2 = new DemoAdapter(title);
DemoAdapter demoAdapter2 = new DemoAdapter(title) {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mMainTv2.append("onCreateViewHolder\n");
return super.onCreateViewHolder(parent, viewType);
}
};
mMainRecycle2.setAdapter(demoAdapter2);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package github.hellocsl.gallerylayoutmanager.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import github.hellocsl.gallerylayoutmanager.BuildConfig;
import github.hellocsl.gallerylayoutmanager.R;


/**
* Created by chensuilun on 2016/11/15.
*/
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.ViewHolder> implements View.OnClickListener {

private static final String TAG = "DemoAdapter";
private List<String> items;
private OnItemClickListener mOnItemClickListener;

Expand All @@ -30,15 +32,21 @@ public DemoAdapter setOnItemClickListener(OnItemClickListener onItemClickListene

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (BuildConfig.DEBUG) {
Log.e(TAG, "onCreateViewHolder: ");
}
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_demo, parent, false);
v.setOnClickListener(this);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onBindViewHolder: position:" + position);
}
String item = items.get(position);
holder.text.setText(item);
holder.text.setText("HelloWork:" + item);
holder.itemView.setTag(position);
}

Expand Down
Loading

0 comments on commit eb2abbb

Please sign in to comment.