[木木方文安卓學(xué)習(xí)筆記一]ListView列表

1耕肩、取到Fragment_list,放入activity_main的content中

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fragment_list frist = new Fragment_list();
        //獲取到低版本兼容的
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.content, frist, "frist");
        transaction.commit();
        //對(duì)fragment進(jìn)行任何操作都必須提交

        //fragment傳值
//        Bundle bundle = new Bundle();
//        bundle.putString("title", "香菇藍(lán)瘦");
//        frist.setArguments(bundle);
        //3.0 above
//        getFragmentManager();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
    xmlns:app="[http://schemas.android.com/apk/res-auto](http://schemas.android.com/apk/res-auto)"
    xmlns:tools="[http://schemas.android.com/tools](http://schemas.android.com/tools)"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.huoliquankai.testfragment.MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content"
        android:tag="15"
        >
    </FrameLayout>
</android.support.constraint.ConstraintLayout>

2欲逃、Fragment_list獲取xml布局文件找蜜,傳數(shù)據(jù)到adapter中。

public class Fragment_list extends Fragment {
    ListView mListView;
    ArrayList<String> Strings;
    MyAdapter myAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Strings = new ArrayList<>();
        View view = inflater.inflate(R.layout.fragment_list, null);
        mListView = (ListView) view.findViewById(R.id.listView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.initDate(getActivity());
    }

    private void initDate(Context context) {
        for (int i = 0; i<50; i++) {
            Strings.add("第"+i+"條標(biāo)題稳析!");
        }
        myAdapter = new MyAdapter(Strings, context);
        mListView.setAdapter(myAdapter);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
    xmlns:tools="[http://schemas.android.com/tools](http://schemas.android.com/tools)"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        />

</RelativeLayout>

3洗做、Adapter設(shè)置count和item等(類(lèi)似iOS設(shè)置代理數(shù)據(jù)cell等)

public class MyAdapter extends BaseAdapter {
    ArrayList<String> titles;
    LayoutInflater mInflater;
    public MyAdapter(ArrayList<String> titles, Context context) {
        this.titles = titles;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return titles.size();
    }

    @Override
    public Object getItem(int position) {
        return titles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//類(lèi)似return UITableViewCell
        View view = mInflater.inflate(R.layout.item_title, null);
        TextView title = (TextView) view.findViewById(R.id.titleaaa);
        title.setText(titles.get(position));
        return view;
    }
}

4.item_title類(lèi)似單個(gè)cell

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/titleaaa"
            android:textSize="22sp"
            android:textColor="@android:color/black"/>
    </LinearLayout>
</LinearLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市彰居,隨后出現(xiàn)的幾起案子诚纸,更是在濱河造成了極大的恐慌,老刑警劉巖陈惰,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咬清,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡奴潘,警方通過(guò)查閱死者的電腦和手機(jī)旧烧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)画髓,“玉大人掘剪,你說(shuō)我怎么就攤上這事∧蜗海” “怎么了夺谁?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)肉微。 經(jīng)常有香客問(wèn)我匾鸥,道長(zhǎng),這世上最難降的妖魔是什么碉纳? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任勿负,我火速辦了婚禮,結(jié)果婚禮上劳曹,老公的妹妹穿的比我還像新娘奴愉。我一直安慰自己琅摩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布锭硼。 她就那樣靜靜地躺著房资,像睡著了一般。 火紅的嫁衣襯著肌膚如雪檀头。 梳的紋絲不亂的頭發(fā)上轰异,一...
    開(kāi)封第一講書(shū)人閱讀 51,598評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音暑始,去河邊找鬼溉浙。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蒋荚,可吹牛的內(nèi)容都是我干的戳稽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼期升,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼惊奇!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起播赁,我...
    開(kāi)封第一講書(shū)人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤颂郎,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后容为,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體乓序,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年坎背,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了替劈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡得滤,死狀恐怖陨献,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情懂更,我是刑警寧澤眨业,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站沮协,受9級(jí)特大地震影響龄捡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜慷暂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一聘殖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦就斤、人聲如沸悍募。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至洋魂,卻和暖如春绷旗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背副砍。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工衔肢, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留函荣,地道東北人冷冗。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像养葵,于是被迫代替她去往敵國(guó)和親心剥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子邦尊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容