之前接触了一点ListView的基础知识,但没有自己去敲,学的不是很深刻,今天我按照教程,写了一个listview的基本实现,基本掌握了listview
listview的学习是为了给RecyclerView打一下基础,毕竟RecyclerView优点更多,更好用,实际开发中主要就是用到它。
先看看实现了什么效果:首先我为了省事,直接把每一个单元内的内容设置的一样,之后实际使用的时候肯定不这么弄了~
然后就是可以上下滑动,点击第几个就会弹出: “点击pos:几” 的效果,在实际使用中,我们可以将这个效果改为跳转到对应的界面。
代码实现,先放最主要的部分
MyListAdapter.java
package com.example.testapplication.listview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.testapplication.R; public class MyListAdapter extends BaseAdapter { private Context mContext; private LayoutInflater mLayoutInflater; public MyListAdapter(Context context){ this.mContext = context; mLayoutInflater = LayoutInflater.from(context); } @Override public int getCount() { return 10; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } static class ViewHolder{ public TextView tv1,tv2,tv3; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder = null; if (view == null){ view = mLayoutInflater.inflate(R.layout.layout_list_item,null); holder = new ViewHolder(); holder.tv1=(TextView) view.findViewById(R.id.tv_1); holder.tv2=(TextView) view.findViewById(R.id.tv_2); holder.tv3=(TextView) view.findViewById(R.id.tv_3); view.setTag(holder); }else{ holder = (ViewHolder) view.getTag(); } //给控件赋值 holder.tv1.setText("姓名是"); holder.tv2.setText("班级是"); holder.tv3.setText("学号是"); return view; } }
ListViewActivity.java
package com.example.testapplication.listview; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.example.testapplication.R; public class ListViewActivity extends AppCompatActivity { private ListView mLv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); mLv1 = (ListView) findViewById(R.id.lv_1); mLv1.setAdapter(new MyListAdapter(ListViewActivity.this)); mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ListViewActivity.this, "点击pos:"+position, Toast.LENGTH_SHORT).show(); } }); } }
MainActivity.java
package com.example.testapplication; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.example.testapplication.listview.ListViewActivity; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_listview = (Button) findViewById(R.id.btn_listview); btn_listview.setOnClickListener(this); } @Override public void onClick(View view) { if (findViewById(R.id.btn_listview).equals(view)) { Intent intent = new Intent(this, ListViewActivity.class); startActivity(intent); } } }标签:再视,public,listview,import,Android,ListView,holder,android,view From: https://www.cnblogs.com/rsy-bxf150/p/17180924.html