Set the adapter to set RecyclerView manifestations


public class RecycleViewActivity extends Activity {
@BindView(R.id.recycleview)
RecyclerView recycleview;
List<People> DataList;
@BindView(R.id.removeButton)
Button removeButton;
@BindView(R.id.addButton)
Button addButton;

//定义适配器
private MyAdapter adapter;

public void onCreate(Bundle bunle) {
super.onCreate( bunle );
setContentView( R.layout.recycleviewlayout );
ButterKnife.bind( this );

//初始化子项数组数据
DataList = new ArrayList<People>();
initData();
//设置适配器,同时传入数据
adapter = new MyAdapter( this, DataList );
recycleview.setAdapter( adapter );
// Create an object LinearLayoutManager used herein is meant a linear layout LinearLayoutManager
LinearLayoutManager LayoutManager = new new LinearLayoutManager (the this);

// scroll to child index value, the method requires the use of new LinearLayoutManager (this, LinearLayoutManager.VERTICAL, false ) constructor
// second argument specifies a vertical or horizontal layout, false if the reverse is the
// new new LinearLayoutManager LinearLayoutManager LayoutManager = (the this, LinearLayoutManager.VERTICAL, false);
//layoutmanager.scrollToPosition (. 19);

// set subkey layout is horizontal
//layoutmanager.setOrientation (LinearLayoutManager.HORIZONTAL);

// set RecyclerView layout
recycleview.setLayoutManager (LayoutManager);

// use grid layout, the second parameter is the number of sub-entries in each row have
// gridLayoutManager gridLayoutManager = new gridLayoutManager ( this, 4, GridLayoutManager.HORIZONTAL, false) ;
//recycleview.setLayoutManager(gridLayoutManager);

// add the dividing line, the first parameter context, the second parameter is a layout direction
recycleview.addItemDecoration (new new DividerItemDecoration (the this, DividerItemDecoration.VERTICAL));

// use custom interface external settings click event
adapter.setItemClickListener (new new MyAdapter.ItemClickListener () {
@Override
public void onItemClick (int position) {
IF (position> 2) {
Toast.makeText (RecycleViewActivity.this, "my place is greater than 3", Toast .LENGTH_SHORT) the .Show ();
} the else IF (position == 2) {
Toast.makeText (RecycleViewActivity.this, "I = 3 in place of", Toast.LENGTH_SHORT) the .Show ();
} the else
Toast.makeText (RecycleViewActivity.this, "I place less than 3", Toast.LENGTH_SHORT) .Show ();
}
});
}


// initialization data method, passing in the adapter
Private void initData () {
for (int I = 0; I <. 6; I ++) {
DataList.add (new new People ( "Truth only reliable standard is always self-consistent -. - Owen ", R.drawable.ouwen));
DataList.add (new new People (" the land is fertile and its harvest is valued; also to land, but it is not a production of "+
" food, but the truth If only breed meditation and fantasy, even if another big talent is just sand or salt ponds, that even the grass is not too long out of the above. "
, R.drawable.bielin));
}
}
// add a child and removed child
@OnClick ({R.id.removeButton, R.id.addButton})
public void onViewClicked (View View) {
Switch (view.getId (http://www.my516.com)) {
Case R.id .removeButton:
adapter.removeItem (0);
BREAK;
Case R.id.addButton:
adapter.addItem (0, new new People ( "Add data", R.drawable.bielin));
// need to scroll to add location
recycleview.scrollToPosition( 0 );
break;
}
}
}

Guess you like

Origin www.cnblogs.com/hyhy904/p/11373119.html
set
set