Android ListView 的简单应用包括点击事件

释放双眼,带上耳机,听听看~!

项目当中用到了ListView ,今天抽出时间整理了一下ListView的简单用法。

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener ,AdapterView.OnItemClickListener,AbsListView.OnScrollListener{
private ListView listView;
    private ArrayAdapter<String>arr_adapter;
    private SimpleAdapter simp_adapter;
    private List<Map<String,Object>>dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.listView);
  //这个是数组适配器的数据源 ArrayAdapter
String []arr_data={"逗你玩1","逗你玩2","逗你玩3","逗你玩4","逗你玩5","逗你玩6",};
//简单适配器SimpleAdapter 数据源    Map键值对形式
dataList = new ArrayList<Map<String,Object>>();
arr_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr_data);
simp_adapter = new SimpleAdapter(this,getSimpDataList(),R.layout.simpitems,new String[]{"pic","text"},new int[]{R.id.simp_pic,R.id.simp_text});
//        listView.setAdapter(arr_adapter );
listView.setAdapter(simp_adapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle) ;
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private List<Map<String,Object>> getSimpDataList(){
for(int i=0;i<30;i++){
Map<String ,Object>map = new HashMap<String ,Object>();
map.put("pic",R.mipmap.ic_launcher);
map.put("text","DEMO逗你玩"+i);
dataList.add(map);
}
return dataList;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
        return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
        if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
        return true;
}
//监听点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"点击了"+position,Toast.LENGTH_LONG).show();
}
//手势的监听
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState){
case SCROLL_STATE_FLING:
int i = 0;
Log.i("Main","用户手指在离开屏幕之前,由于用力滑了一下,试图在惯性的情况下接着滑动");
Map<String,Object>map = new HashMap<String,Object>();
map.put("pic",R.mipmap.ic_launcher);
map.put("text","我是增加项"+ ++i);
dataList.add(map);
simp_adapter.notifyDataSetChanged();
                break;
            case SCROLL_STATE_IDLE:
Log.i("Main","视图已经停止滑动");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
Log.i("Main","手指没有离开屏幕,试图正在滑动");
                break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
}

 

SimpAdapterXML布局 很简单

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/simp_pic"
android:layout_marginLeft="15dp"
android:src="https://blog.csdn.net/csdndouniwan/article/details/51361578/@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/simp_text"
android:textSize="20dip"
android:textColor="#000"
android:gravity="center"
android:text="demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

 

人已赞赏
Android文章

Android Sublime 快捷键

2020-3-26 23:02:01

Android文章

Android BaseAdapter的使用与优化

2020-3-27 0:19:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索