标签: android-listfragment

多少活动与碎片?

介绍:

基本的"片段教程"模式是这样的:

  1. 在平板电脑上,左侧有一个列表,右侧有详细信息.
  2. 两者都存在 Fragments并且两者都存在于同一个中Activity.
  3. 在手机上,有一个列表Fragment中的一个Activity.
  4. 推出一个新Activity的细节Fragment.

(例如Dianne Hackborn的Android 3.0 Fragments APIFragments API指南)

在这两个设备上,功能都在Fragments.(简单)

平板电脑上,整个应用程序是1Activity,在手机上,有很多Activities.


问题:

  • 是否有理由将手机应用程序拆分为多个Activities

此方法的一个问题是,您复制了主平板电脑Activity和单独电话中的大量逻辑Activities.

  • 使用相同的切入和切Fragments出逻辑(仅使用不同的布局),在两种情况下保留1活动模型会不会更容易?

这种方式大部分逻辑都存在于Fragments自身中,并且只有一个Activity- 更少的代码重复.

我所读到的ActionBarSherlock也是它似乎最好用Fragments而不是Activities(但我还没有使用它).

教程是否过于简化,或者我错过了这种方法的主要内容?


我们已经在办公室成功地尝试了这两种方法 - 但我即将开始一个更大的项目,并希望尽可能让自己变得容易.

一些相关问题的链接:

android android-fragments android-listfragment

184
推荐指数
3
解决办法
3万
查看次数

作为ViewPager的一部分更新ListFragment中的数据

我正在使用Android中的v4兼容性ViewPager.我的FragmentActivity有一堆数据,可以在我的ViewPager中的不同页面上以不同的方式显示.到目前为止,我只有3个相同的ListFragment实例,但将来我将有3个不同的ListFragments实例.ViewPager位于垂直电话屏幕上,列表不是并排的.

现在,ListFragment上的一个按钮启动一个单独的整页活动(通过FragmentActivity),它返回并且FragmentActivity修改数据,保存数据,然后尝试更新其ViewPager中的所有视图.就在这里,我被卡住了.

public class ProgressMainActivity extends FragmentActivity
{
    MyAdapter mAdapter;
    ViewPager mPager;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    ...
        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager) findViewById(R.id.viewpager);
        mPager.setAdapter(mAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        ...
        updateFragments();
        ...
    }
    public void updateFragments()
    {
        //Attempt 1:
        //mAdapter.notifyDataSetChanged();
        //mPager.setAdapter(mAdapter);

        //Attempt 2:
        //HomeListFragment fragment = (HomeListFragment) getSupportFragmentManager().findFragmentById(mAdapter.fragId[0]);
        //fragment.updateDisplay();
    }

    public static class MyAdapter extends FragmentPagerAdapter implements
         TitleProvider
    {
      int[] fragId = {0,0,0,0,0};
      public MyAdapter(FragmentManager fm)
      {
         super(fm);
      }
      @Override …
Run Code Online (Sandbox Code Playgroud)

android android-adapter android-fragments android-viewpager android-listfragment

141
推荐指数
5
解决办法
7万
查看次数

getLoaderManager().initLoader()不接受'this'作为参数,尽管类(ListFragment)实现了LoaderManager.LoaderCallbacks <Cursor>

我在遵循Android中使用SQLite 的指南时遇到了麻烦.我正在使用a ListFragment而不是ListActivity(如示例中所示),所以我使用了ListFragment工具LoaderManager.LoaderCallbacks<Cursor>.然后,在以下fillData()方法中ListFragment:

private void fillData() {
    // Fields from the database (projection)
    // Must include the _id column for the adapter to work
    String[] from = new String[] { NotesSQLiteHelper.COLUMN_TITLE };
    // Fields on the UI to which we map
    int[] to = new int[] { R.id.label };

    getLoaderManager().initLoader(0, null, this); //error
    adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.notes_row, null, from, to, 0);
    setListAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

The method …
Run Code Online (Sandbox Code Playgroud)

sqlite android android-listfragment android-loadermanager

64
推荐指数
3
解决办法
5万
查看次数

getSupportFragmentManager()和getChildFragmentManager()有什么区别?

我的类继承了Fragment,这就是为什么它不能使用getSupportFragmentManager().我正在使用getChildFragmentManager,它显示错误 - IllegalArguementException:找不到id ...错误的视图.

任何指导将不胜感激.

调用AttachmentsListFragment的代码是

Bundle b = new Bundle();
b.putSerializable("AttachmentsList", msg.attachments);  
        AttachmentListFragment listfrag = new AttachmentListFragment(msg.attachments);
FragmentTransaction transaction = getFragmentManager().beginTransaction();       
transaction.add(R.id.attachmentslistcontainer, listfrag);
transaction.addToBackStack(null);
transaction.commit();
Run Code Online (Sandbox Code Playgroud)

attachmentslayout.xml是

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/attachmentslistcontainer"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewAttachmentHeader"
        style="@style/Normal.Header.Toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/list_separator_background"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="2"
        android:text="@string/attachments_header"
        android:textColor="#FFFFFFFF"
        android:textSize="22sp"
        android:textStyle="bold"
        android:visibility="visible" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

AttachmentsListFragment.java

public class AttachmentListFragment extends ListFragment implements IAttachmentsData {

    ArrayList<Attachments> items = null;
    Integer cellLayoutID;
    Integer index;

    public AttachmentListFragment() {

    } …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-listfragment

64
推荐指数
3
解决办法
4万
查看次数

Android片段之间的阴影分隔符

我的布局类似于平板电脑的ICS Gmail应用程序(ListFragment左侧和右侧内容),我想知道如何构建布局,以便在两个片段之间存在阴影分隔符(例如在Gmail中)应用程序,如下所示)

.只要看看那美丽的影子!

此外,由于这适用于此问题,如何在活动列表项的布局中具有漂亮的三角形/箭头标记?我假设要实现这一点,ListView本身必须位于阴影"层" 之上,但我不知道如何创建它.

android android-layout android-fragments android-listfragment

37
推荐指数
2
解决办法
1万
查看次数

错误膨胀类片段 - 重复id/illegalargumentexception?

我正在尝试创建一个我正在构建的应用程序从主要活动中获取搜索词,返回结果,然后使结果可以被点击,以便可以从每个结果中查看详细信息.我这样做是通过使用MainActivity,ResultsActivity和PlaceActivity,然后是ListFragmentClickable(扩展ListFragment).如果手机朝向纵向模式,则结果列表应该是可见的,只有在单击结果时才会查看详细信息.如果手机是横向的,则在选择项目时,应在列表右侧弹出详细信息窗口.

在运行时,我收到一个错误,上面写着"错误膨胀类片段".我不知道是什么导致它,我很乐意帮助摆脱它.

我的ListFragmentClickables由我的ResultsActivity调用,它位于:

public class ResultsActivity extends FragmentActivity implements ListFragmentClickable.OnItemSelectedListener{

private ArrayAdapter<String> mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results_view);

    //Receive searchTerm from MainActivity
    Intent intent = getIntent();
    String searchTerm = intent.getStringExtra(MainActivity.SEARCH_TERM);

    mAdapter = new ArrayAdapter<String>(this, R.layout.item_label_list);

    FragmentManager     fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    FactualResponderFragment responder = (FactualResponderFragment) fm.findFragmentByTag("RESTResponder");
    if (responder == null) {
        responder = new FactualResponderFragment();
        ft.add(responder, "RESTResponder");
    }

    Bundle bundle = new Bundle();
    bundle.putString("search_term", searchTerm);
    responder.setArguments(bundle);

    ft.commit();
}

public ArrayAdapter<String> getArrayAdapter() {
    return mAdapter;
} …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-listfragment

26
推荐指数
4
解决办法
2万
查看次数

Android从其父活动刷新片段列表

我有一个主活动,其中包含带有3个菜单按钮的操作栏.

然后我在这个主要活动中有一个片段,里面有一个列表.

我希望能够刷新主活动片段中的列表,当单击其中一个菜单按钮时,或者最好只删除列表中的所有行.

任何帮助表示赞赏.

谢谢.

public class Favourite extends SherlockFragmentActivity {

    ActionBar actionBar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favourite);

        actionBar = getSupportActionBar();

        actionBar.setDisplayShowTitleEnabled(false);

        BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.actionbar_bg);
        bg.setTileModeX(TileMode.REPEAT); 
        getSupportActionBar().setBackgroundDrawable(bg);

        getSupportActionBar().setIcon(R.drawable.favourite_title);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.Tab tabAll = actionBar.newTab();
        ActionBar.Tab tabfavs = actionBar.newTab();
        ActionBar.Tab tabhist = actionBar.newTab();
        tabAll.setText("all");
        tabfavs.setText("favs");
        tabhist.setText("hist");
        tabAll.setTabListener(new MyTabListener());
        tabfavs.setTabListener(new MyTabListener());
        tabhist.setTabListener(new MyTabListener());
        actionBar.addTab(tabAll);
        actionBar.addTab(tabfavs);
        actionBar.addTab(tabhist);


        try{

        }
        catch(Exception e)
        {
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.actionbar_itemlist_favourite, menu);

        return true;
    }

    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-listfragment

18
推荐指数
3
解决办法
2万
查看次数

ListFragment onItemClickListener无法正常工作

我正在使用选项卡式布局(使用滑动).这里我有3个选项卡,由SectionsPagerAdapter控制.每个选项卡都是一个ListFragment.

现在,我希望在单击列表中的某个项目时触发事件.我希望每个标签都有一个监听器.

这是现在的代码(哪个不起作用,事件不会被触发).

public class NyhederFragment extends ListFragment {
     public static final String ARG_SECTION_NUMBER = "section_number";
     private static final String TAG="NyhederFragment";
     private List<Item> newsItems;
     private ArrayList newsHeadlines;
     private ArrayAdapter adapter;
     private BroadcastReceiver updateReciever;

public NyhederFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ListView newsList = new ListView(getActivity());
    newsList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    newsList.setId(R.id.list);
    DatabaseHelper dbConn = new DatabaseHelper(getActivity());

    newsItems = dbConn.getAllItemsFromNews();
    newsHeadlines = new ArrayList();
    for(Item i : newsItems){
        newsHeadlines.add(i.getTitle());
    }

    adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, newsHeadlines); …
Run Code Online (Sandbox Code Playgroud)

android android-listfragment

15
推荐指数
2
解决办法
9427
查看次数

片段已经激活 - 尝试setArguments时

我正在使用以下链接中的示例给出

http://android-er.blogspot.in/2013/04/handle-onlistitemclick-of-listfragment.html

这里我有两个类,一个扩展List Fragment和其他扩展Fragment.现在我以这种方式将对象传递给detailfragment:

*来自ListFragment*

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);

        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_KEY, obj);// passing this object

        detailFragment.setArguments(bundle);
        detailFragment.setUpLayout();// update the UI
} 
Run Code Online (Sandbox Code Playgroud)

现在在Fragment类中我收到它,基本目标是根据列表片段中选择的项更新片段的UI,这就是我发送对象的原因

Bundle b = getArguments();
b.getSerializable(BUNDLE_KEY);
Run Code Online (Sandbox Code Playgroud)

现在选择项目时,它表示"片段已经处于活动状态".

这是什么问题?我究竟做错了什么?

android fragment android-fragments android-listfragment

15
推荐指数
3
解决办法
2万
查看次数

ListFragment下面的固定且始终可见的页脚

我正在尝试将一个固定且始终可见的页脚附加到ListFragment的底部.

我现在这样做:

@Override public void onActivityCreated(Bundle savedInstanceState) {

    // ...

    adapter = new MyAdapter(getActivity(), R.layout.list, dataList);

    ListView list = getListView();
    View footer = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_add, null, false);

    list.addFooterView(footer);
    setListAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)

虽然这段代码确实在列表的底部生成了一个视图,但它并没有真正做到我想要的:

首先,我需要页脚固定,即在屏幕上可见,无论列表的滚动位置如何.使用此解决方案,只有当屏幕滚动到列表底部时,才会显示页脚.

其次,即使列表为EMPTY,我也需要显示页脚.在此解决方案中,当列表为空时,页脚不可见.

固定页脚(在我的情况下,按钮)总是出现在ListFragment或ListActivity下面的最佳方法是什么?

谢谢!

android android-listview android-listfragment

14
推荐指数
1
解决办法
1万
查看次数