您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Android適配器的概念是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
在常見(jiàn)軟件中,往往能看到列表一樣的信息流,例如:
如果在xml中將信息寫(xiě)死,那顯然是不合適的,無(wú)法做到上拉刷新的效果。
這個(gè)時(shí)候,就需要用到適配器。
Android中有很多的適配器,首先看看這些適配器的繼承結(jié)構(gòu)
這些適配器中,BaseAdapter用的最多,也用的最熟
main.xml:
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> </ListView>
listview_item.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center_vertical" android:orientation="horizontal" android:padding="15dp"> <ImageView android:id="@+id/img_icon" android:layout_width="130dp" android:layout_height="80dp" android:src="@mipmap/news"/> <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_weight="1"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="我是一個(gè)新聞標(biāo)題---- 1" android:textColor="#000000" android:textSize="18dp" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="我是新聞內(nèi)容---- 1" android:textColor="#000000" android:textSize="14dp" /> </RelativeLayout> </LinearLayout>
MainActivity
package cn.edu.cdut.testadapter; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import java.util.LinkedList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<News> mData = null; private Context mContext; private NewsAdapter mAdapter = null; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; listView = (ListView) findViewById(R.id.listview); mData = new LinkedList<News>(); for (int i = 0; i < 10; i++) { mData.add(new News("我是一個(gè)新聞標(biāo)題---- " + i, "我是一個(gè)新聞內(nèi)容---- " + i, R.mipmap.news)); } mAdapter = new NewsAdapter(mData, mContext); listView.setAdapter(mAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(mContext, "點(diǎn)擊了第" + position + "條數(shù)據(jù)", Toast.LENGTH_SHORT).show(); } }); } }
News.java
package cn.edu.cdut.testadapter; public class News { private String title; private String content; private int aIcon; public News() { } public News(String title, String content, int aIcon) { this.title = title; this.content = content; this.aIcon = aIcon; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getaIcon() { return aIcon; } public void setaIcon(int aIcon) { this.aIcon = aIcon; } }
NewsAdapter
package cn.edu.cdut.testadapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class NewsAdapter extends BaseAdapter { private List<News> mData; private Context mContext; public NewsAdapter(List<News> mData, Context mContext) { this.mData = mData; this.mContext = mContext; } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, parent, false); ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon); TextView title = (TextView) convertView.findViewById(R.id.tv_title); TextView content = (TextView) convertView.findViewById(R.id.tv_content); img_icon.setBackgroundResource(mData.get(position).getaIcon()); title.setText(mData.get(position).getTitle()); content.setText(mData.get(position).getContent()); return convertView; } }
“Android適配器的概念是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。