欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Fragment实现列表和内容联动

程序员文章站 2022-07-04 09:53:00
在平板上经常能看到这种的情况:左边是一个列表,右边是列表项对应的内容,当点击某一个列表时,右边内容区也会随之改变。下面使用fragment简单的demo: 思路:在mai...

在平板上经常能看到这种的情况:左边是一个列表,右边是列表项对应的内容,当点击某一个列表时,右边内容区也会随之改变。下面使用fragment简单的demo:

思路:在mainactivity定义一个回调接口,并在列表点击回调该接口,然后在内容区的fragment设置回调,同时改变fragment的显示内容。具体代码如下:

主布局:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginleft="16dp"
  android:layout_marginright="16dp"
  android:divider="?android:attr/dividervertical"
  android:showdividers="middle">

  <listview
    android:id="@+id/book_list"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

  <framelayout
    android:id="@+id/book_detail_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"/>

</linearlayout>

listview的item布局和内容区fragment的布局都比较简单,分别是一个textview和两个textview布局,这里不再贴出。

mainactivity类(内容区的数据是写死的,仅仅做演示作用,一般来说需要从网络上获取数据):

public class mainactivity extends activity
{
  private onchangelistener onchangedlistener;
  list<book> books;
  list<string> book_name = new arraylist<>();
  listview listview;
  adapter adapter;
  @override
  protected void oncreate(bundle savedinstancestate)
  {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    books = new arraylist<>();
    initdata();
    for (int i = 0 ; i < books.size() ; i++)
    {
      book_name.add(books.get(i).getname());
    }

    listview = (listview) findviewbyid(r.id.book_list);
    adapter = new adapter(book_name , this);
    listview.setadapter(adapter);

    myfragment fragment = new myfragment();
    fragmentmanager fragmentmanager = getfragmentmanager();
    fragmenttransaction transaction = fragmentmanager.begintransaction();
    transaction.replace(r.id.book_detail_container , fragment);
    transaction.commit();

    listview.setonitemclicklistener(new adapterview.onitemclicklistener()
    {
      @override
      public void onitemclick(adapterview<?> parent, view view, int position, long id)
      {
        adapter.setselectedposition(position);
        adapter.notifydatasetinvalidated();

        if (onchangedlistener != null)
        {
          onchangedlistener.changetext(books.get(position));
        }
      }
    });
  }

  private void initdata()
  {
    book book1 = new book();
    book1.setid(1);
    book1.setname("疯狂java讲义");
    book1.setdesc("一本全面的,深入的java学习图书,受到广大读者的热烈喜爱,已经被多家高校选为教材,成为国内一线的java学习素材");
    books.add(book1);

    book book2 = new book();
    book2.setid(2);
    book2.setname("疯狂android讲义");
    book2.setdesc("android学习者的首选教材,常年占据京东,当当,亚马逊3大网店android销量排行榜首,是人人可必备的android入门图书");
    books.add(book2);

    book book3 = new book();
    book3.setid(3);
    book3.setname("java ee企业应用实战");
    book3.setdesc("全面介绍java ee开发的struts2 , spring 3 , hibernate 4 架构,可以通过该书系统学习企业级开发,全面掌握java学习");
    books.add(book3);

    book book4 = new book();
    book4.setid(4);
    book4.setname("html5开发");
    book4.setdesc("全面介绍html5,css ,javascript等知识点,通过该书可以系统的学习web前端开发,通常javascript脚本设置动态页面");
    books.add(book4);
  }

  public void setonchangelistener(onchangelistener onchangelistener)
  {
    this.onchangedlistener = onchangelistener;
  }

  interface onchangelistener
  {
    void changetext(book book);
  }
}

列表的adapter类:

public class adapter extends baseadapter
{
  private list<string> mdata;
  private layoutinflater layoutinflater;
  private int selectionposition = -1;
  public adapter(list<string> data , context context)
  {
    this.mdata = data;
    this.layoutinflater = layoutinflater.from(context);
  }

  @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 viewgroup)
  {
    viewholder viewholder = null;
    if (convertview == null)
    {
      viewholder = new viewholder();
      convertview = layoutinflater.inflate(r.layout.item_layout , null);
      viewholder.tv = (textview) convertview.findviewbyid(r.id.item_text);
      convertview.settag(viewholder);
    }
    else
    {
      viewholder = (viewholder) convertview.gettag();
    }

    viewholder.tv.settext(mdata.get(position));
    if (selectionposition == position)
    {
      viewholder.tv.setbackgroundcolor(color.yellow);
    }
    else
    {
      viewholder.tv.setbackgroundcolor(color.white);
    }
    return convertview;
  }

  public void setselectedposition(int position)
  {
    this.selectionposition = position;
  }

  class viewholder
  {
    textview tv;
  }
}

内容区的fragment类:

public class myfragment extends fragment
{
  mainactivity activity;
  private textview title;
  private textview desc;
  @nullable
  @override
  public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate)
  {
    view view = inflater.inflate(r.layout.fragment_book_detail , null);
    title = (textview) view.findviewbyid(r.id.book_title);
    desc = (textview) view.findviewbyid(r.id.book_desc);
    log.d("fragment" , "created");
    activity = (mainactivity) getactivity();
    activity.setonchangelistener(new mainactivity.onchangelistener()
    {
      @override
      public void changetext(book book)
      {
        title.settext(book.getname());
        desc.settext(book.getdesc());
      }
    });
    return view;
  }
}

book实体类:

public class book
{
  private int id;
  private string name;
  private string desc;
  public book()
  {
  }

  public book(int id , string name , string desc)
  {
    this.id = id;
    this.name = name;
    this.desc = desc;
  }

  public void setname(string name) {
    this.name = name;
  }

  public void setid(int id) {
    this.id = id;
  }

  public void setdesc(string desc) {
    this.desc = desc;
  }

  public int getid() {
    return id;
  }

  public string getname() {
    return name;
  }

  public string getdesc() {
    return desc;
  }
}

效果:

Android Fragment实现列表和内容联动

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。