标签: android-listfragment

创建自定义简单光标适配器

我想创建一个非常简单的游标自定义游标适配器,以方便在点击时更改行项目的颜色.使用以下代码

private static int save = -1;

public void onListItemClick(ListView parent, View v, int position, long id) { 

    parent.getChildAt(position).setBackgroundColor(Color.BLUE);

    if (save != -1 && save != position){
        parent.getChildAt(save).setBackgroundColor(Color.BLACK);
    }

    save = position;                

}
Run Code Online (Sandbox Code Playgroud)

我从这个线程获得了代码/sf/answers/535491631/

我会使用一个简单的游标适配器并将代码放在onClick中,但由于ListFragment中的默认列表重用了视图,因此滚动多个视图时会突出显示.谈到IRC,有人建议我创建一个自定义游标适配器.但是,我似乎无法找到如何执行此操作的最佳实践,以及上述代码段所适用的位置.可以非常感谢帮助.

public class AreaCursorAdapter extends CursorAdapter {
    private Context context;


    public AreaCursorAdapter(Context context, Cursor c) {
        super(context, c);
        // TODO Auto-generated constructor stub
    }

    @Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView list_item = (TextView)view.findViewById(android.R.id.text1);
    list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME)));

}

@Override
public View newView(Context context, …
Run Code Online (Sandbox Code Playgroud)

android android-listview simplecursoradapter android-listfragment android-cursoradapter

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

listfragment中的第二个listview不会滚动

我创建了一个应用程序,其中有两个片段,在两个片段中都有listviews.fragment1中的第一个listview正在滚动,项目也会突出显示.但是在第二个片段中,列表视图不会滚动,甚至项目也不会突出显示.谁能告诉我这是什么问题?这里的事情是我只是通过将相同的片段类放到xml中的两个片段来检查它.要么它们都应该工作,要么两者都不应该因为一个与另一个没有区别.但为什么会出现这个问题?

我的片段类:

public class Fragment1 extends ListFragment{

    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment1,container,false);
    }
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,countries);
        setListAdapter(adapter);
    }

    public void onListItemClick(ListView parent, View v,int position, long id)
    {
        Toast.makeText(getActivity(), "You have selected "+countries[position], Toast.LENGTH_SHORT).show();
    }

}
Run Code Online (Sandbox Code Playgroud)

main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:name="com.example.listfragmentexample.Fragment1"
        android:id="@+id/fragment1"
        android:layout_weight="0.5"
        android:layout_width="0dp" …
Run Code Online (Sandbox Code Playgroud)

android scroll android-listfragment

5
推荐指数
1
解决办法
1385
查看次数

以编程方式隐藏/显示上下文操作栏

我的用例:

我有一个活动,其中有一个ViewPager作为唯一的视图元素,Fragment为其三个页面设置了三个s(使用a FragmentPagerAdapter).其中一个片段是a ListFragment,而另外两个常规Fragments包含一些信息图表.

在列表中ListFragment允许"检查"元素,并检查元素带来了上下文操作栏,呈现的操作的列表可能选定/检查的项目.

我的问题:

当我选择几个列表项时,CAB会显示出来.但是当我切换页面时,CAB保持可见,呈现动作.更糟糕的是,ViewPager似乎有时会破坏列表片段的视图 - 因此选择操作会引发"java.lang.IllegalStateException: Content view not yet created"异常.

我的问题:

  1. 当用户离开ListFragment页面时,如何让CAB消失?
  2. (可选)当ListFragment背面和某些项目仍然被检查时,如何让CAB重新出现.

android android-viewpager android-listfragment contextual-action-bar

5
推荐指数
1
解决办法
1512
查看次数

Curser适配器中的notifyDataSetChanged不使用listfragment

我有一个listfragment,显示数据库中的列表.除了一件我无法清理的小物品外,我还能正常工作.列表视图看起来像这样.

-----------------------------------------------------
text text text | some more text | delete row button |
-----------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我的问题:当用户按下删除行按钮行从数据库中删除,但不会从屏幕上,直到活动被停止并再次启动被删除.

我知道这notifyDataSetChanged()是更新这些列表的首选方法,但它似乎没有做任何事情......无论如何,下面是我notifyDataSetChanged()调用的游标适配器的代码.

public class CalcCursorAdapter extends SimpleCursorAdapter{

    private Context mContext;
    private ListView mListView;
    private int mLayout;
    private Cursor mcursor;

    protected static class ViewHolder {
        protected TextView text;
        protected ImageButton button;
        private int position;
      }


    @SuppressWarnings("deprecation")
    public CalcCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) 
    {
        super(context, layout, c, from, to);
        this.mContext = context;
        this.mLayout = layout;
        this.mcursor = c; …
Run Code Online (Sandbox Code Playgroud)

android android-listfragment

5
推荐指数
1
解决办法
4667
查看次数

保留searchView状态的片段中的方向更改

我正在尝试使用android开发者的博客条目后面的actionbarcompat(与android支持库).

基本上,我正在尝试创建一个扩展ListFragment的Fragment.我正在使用ArrayAdapter作为列表视图.我能够成功地将操作栏compat与搜索菜单项集成,并且可以使搜索工作正常.

我现在也希望保留片段在方向变化上的状态.我没有在我的片段中设置setRetainInstance(true).为了保留搜索视图的状态,我尝试了以下方法:

  • 在onSaveInstanceState()中保存SearchView中的文本
  • 在onCreateView中,检索searchText(如果可用)
  • 在onCreateOptionsMenu(将在每个方向更改时调用),我试图将搜索查询设置为SearchView实例 mSearchView.setQuery(mSearchText, false);

我用这种方法看到了两个问题:

  1. 在方向更改时调用onQueryTextChange()两次 - 一次使用已保留的searchText(因为mSearchView.setQuery(mSearchText, false);),再次使用空String值.使用空String值的第二次调用更新列表适配器以使所有项目不进行任何过滤.我也不确定为什么会这样.
  2. mSearchView.setQuery(mSearchText, false); 没有设置在搜索查看查询,并没有在UI中可见,以及(与方向的变化,搜索视图默认扩展,重点是没有任何价值的文字,虽然我已经设置了查询).

我的片段大纲如下:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // create and initialize list adapter
    ....
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.plainlist, container, false);
    if (savedInstanceState != null) {
        mSearchText = savedInstanceState.getString(RetainDataKeySearchText, null);
    }
    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    if (isAdded()) {
        if (mSearchView != null) …
Run Code Online (Sandbox Code Playgroud)

android-listfragment android-support-library

5
推荐指数
1
解决办法
3374
查看次数

无法在ListFragment上设置自定义列表视图

我正在尝试在ListFragment上设置自定义Listview布局,我一直在重新研究,我仍然无法让它工作.我是android的新编程,所以我非常感谢你的帮助.

这是我的listFragment类

public class ListCins  extends ListFragment {
public static ArrayList<Cin> cins;
private ListView listView;
private pListAdapter mAdapter;

public static ListCins newInstance() {
ListCins f = new ListCins();
return f;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) { 
super.onActivityCreated(savedInstanceState);
//Los fragments soportan presentar un msj cuando la lista esta vacia.
setEmptyText("Nothing yet, but working");    

}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);




cins = new ArrayList<Cin>();
cins.add(new Cin("Agenter", 1));
cins.add(new Cin("Ora", 2));
cins.add(new Cin("Vistia", 2));
cins.add(new Cin("Bluel", 2));


mAdapter = new …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-listview android-fragments android-listfragment

5
推荐指数
1
解决办法
5760
查看次数

如何在FragmentPagerAdapter上以编程方式更改片段?

我已经使用Eclipse默认的ViewPager应用程序参数创建了应用程序.主要活动包含2个选项卡(tabA和tabB).每个标签指的是其片段(fragmentA和fragmentB).

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new Cars();
        case 1:
            // Games fragment activity
            return new News();
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        } …
Run Code Online (Sandbox Code Playgroud)

android android-fragments fragmentpageradapter android-tabs android-listfragment

5
推荐指数
0
解决办法
688
查看次数

增加ListFragment内置的空文本视图大小

我在我的应用程序中使用listfragment.当数据加载失败时,我使用失败消息调用setEmptyText.这在4英寸手机上运行良好,但在7英寸和10英寸手机上,空文本视图的大小非常小,难以阅读.

4英寸空文本视图

在此输入图像描述

7英寸空文本视图

在此输入图像描述

如何增加空文本视图的大小?

android textview android-listfragment

5
推荐指数
1
解决办法
1032
查看次数

如何获取从其适配器类扩展ListFragment的活动的上下文?

这是我想要调用startActionMode的适配器类.我在setActionMode方法中调用它但得到了以下错误:

  1. 无法从Context转换为ActivityFragment.

  2. 对于ActivityFragment类型,方法startActionMode(ActivityFragment.ActionModeCallback)未定义.

public class ListAdapter extends ArrayAdapter<ListGettersSetters>
{
    ArrayList<ListGettersSetters> arrayListGettersSetters;
    LayoutInflater layoutInflater;
    Context context;
    int Resource, i = 0, j = 0, checkedItemsCount = 0;
    Animation animation1;
    Animation animation2;
    CheckBox flipCheckBox;
    viewHolder holder;
    ActionMode actionMode;
    boolean isActionModeShowing;

    static class viewHolder
    {
        public CheckBox imageView;
        public TextView textViewName;
        public TextView textViewData;
        public TextView textViewDetails;
    }   

    public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters) 
    {
        super(context, resource, arrayListGettersSetters);
        this.arrayListGettersSetters = arrayListGettersSetters;
        Resource = resource;
        this.context = context;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        animation1 …
Run Code Online (Sandbox Code Playgroud)

android android-arrayadapter android-context android-listfragment

5
推荐指数
1
解决办法
996
查看次数

屏幕变暗但对话框未显示

我使用适配器在Fragment中创建了一个列表.我想在单击列表时为列表中的每个项目显示不同的对话框.我已将监听器添加到列表项中,并为其中一个项(第二个)编写了对话框的代码.但是,当我单击该项目时,手机屏幕会变暗但对话框不会显示.我在下面添加了相关代码.我在互联网上搜索了很多关于这个问题的内容,并且自己尝试了很多不同的东西,但似乎没什么用.应用程序中的其他所有工作正常.

任何形式的帮助将非常感激.提前致谢!

DialogFragment类:

public class GenreDialogFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    AlertDialog dialog = builder.create();
    builder.setTitle(R.string.title_genre_dialog);
    builder.setMessage("Are you sure?");
    builder.setPositiveButton("OK",  new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // on success
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    return dialog;
}
}
Run Code Online (Sandbox Code Playgroud)

Parent Fragment Class,我想在其中显示对话框:

public class FragmentAddWatchedMovie extends ListFragment{
@Override
public void onViewCreated(View view, …
Run Code Online (Sandbox Code Playgroud)

android dialog android-fragments android-dialogfragment android-listfragment

5
推荐指数
1
解决办法
858
查看次数