我有一个listview在ListFragment其被从适配器填充,我想强调在列表中点击(或选择)项目,以及以此为基础进行选择的一些动作.我能够处理事件,但如何从列表中设置所选项目的颜色.
我正在修改现有的应用程序以使用ListFragment而不是ListView.
我无法使用JAVA设置分隔符.没有异常被抛出.显示ListFragment没有分隔符.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
lv = getListView();
lv.setTextFilterEnabled(true);
lv.setCacheColorHint(Color.TRANSPARENT);
lv.setFastScrollEnabled(true);
lv.setDividerHeight(1);
lv.setDivider(getActivity().getResources().getDrawable(android.R.color.black));
lv.setBackgroundDrawable(getResources().getDrawable(R.drawable.merge)); //white background
lv.setScrollingCacheEnabled(false);
lv.setSmoothScrollbarEnabled(false);
setListAdapter(new IconicAdapter());
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?非常感谢您的帮助.
我有一个FragmentPagerAdapter创建它的子页面(ListFragments如下):
@Override
public Fragment getItem(int position) {
return FragmentChildPage.newInstance();
}
Run Code Online (Sandbox Code Playgroud)
我给ViewPager一个OffscreenPageLimit1.孩子ListFragments有一个习惯ScrollListener.
我遇到的问题是自定义滚动侦听器仅适用于ListFragments在浏览ViewPager时创建的子窗口OffscreenPageLimit,即子页面> 2.
更令人困惑的是,如果我滚动几页然后返回到第一页,则滚动侦听器现在可以工作.
这里发生了什么?这似乎ViewPager干扰了一些子页面OnScrollListener.
谢谢
由于PreferenceFragment在支持库中没有提供,因此我创建了一个ListFragment向用户显示设置列表,因为我没有很多要显示的设置.我还创建了一个自定义ArrayAdapter来自定义列表项.当用户检查其中一个CheckBoxs 时我需要处理,以便我可以保存天气或者不检查它.因此,如果检查它,它将保持检查,直到用户取消选中它.如果列表中只有一个设置,但现在有2个,我可能需要添加更多设置,这会容易得多.所以我需要能够确定检查了哪一个.我可以处理检查并取消选中,我无法找到确定检查哪一个的方法.
代码
这是我的清单项目:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal">
<TextView android:id="@+id/pref_edit_text" android:layout_width="0dp" android:layout_height="30dp"
android:layout_weight="5"/>
<CheckBox android:id="@+id/pref_check_box" android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1" android:onClick="onCheckBoxClick"/>
</LinearLayout>
<TextView android:id="@+id/pref_edit_text2" android:layout_width="match_parent"
android:layout_height="50dp"/>
Run Code Online (Sandbox Code Playgroud)
这是getView()我的适配器:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//mIntCheckBoxPosition = position;
Typeface tf = Typeface.createFromAsset(mMainActivity.getAssets(), "fonts/ArchitectsDaughter.ttf");
LayoutInflater inflater = mMainActivity.getLayoutInflater();
View view = inflater.inflate(mIntLayoutId, parent, false);
TextView text = (TextView) view.findViewById(R.id.pref_edit_text);
text.setText(mStringArrayTitle[position]);
text.setTypeface(tf, Typeface.BOLD);
text = (TextView) …Run Code Online (Sandbox Code Playgroud) android android-preferences android-arrayadapter android-listfragment android-support-library
我以前使用过这段代码来获得两个布局的滑动选项卡视图,它可以正常工作.
public class MainActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用ListFragment并且我想使它为列表中的每个项目都有一个OnItemClick侦听器但是当我添加时ListView lv = getListView();我得到一个错误.如果我删除了listview的工作但是那时我的应用程序将不需要onclick监听器.
以下是我的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
//...
ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost });
setListAdapter(adapter);
ListView lv = getListView();
//HERE IS WHERE I AM GETTING THE ERROR
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// …Run Code Online (Sandbox Code Playgroud) 我正在开发一个由3个List片段组成的Android应用程序.我正在使用一个ViewPager对象让用户在片段之间滑动.我在片段中使用了相同的代码.但是当我改变它以使用列表片段时,我收到了一个错误
the return type is incompatible with FragmentPagerAdapter.getItem(int)
Run Code Online (Sandbox Code Playgroud)
我被困在这里几个小时.请帮忙..
我的片段活动
public class PageViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<ListFragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
private List<ListFragment> getFragments(){
List<ListFragment> fList = new ArrayList<ListFragment>();
fList.add(AllMsgFragment.newInstance("Fragment 1"));
fList.add(ErrorMsgFragment.newInstance("Fragment 2"));
fList.add(SuccessMsgFragment.newInstance("Fragment 3"));
return fList;
}
Run Code Online (Sandbox Code Playgroud)
和我的FragmentPageAdapter
class MyPageAdapter extends FragmentPagerAdapter {
private List<ListFragment> fragments;
public MyPageAdapter(FragmentManager fm, List<ListFragment> fragments) {
super(fm);
this.fragments = fragments;
} …Run Code Online (Sandbox Code Playgroud) android android-fragments fragmentpageradapter android-listfragment
我ListFragment使用默认设置创建了一个自定义布局ArrayAdapter。我的问题是,当列表不为空时,每行上都会显示空消息(当列表为空时,该消息仅按预期显示一次)。
headline_list.xml(即我的自定义布局文件)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@id/android:list" />
<TextView android:id="@+id/my_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@id/android:empty"
android:text="There are no data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
HeadlineFragment.java
public class HeadlineFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.headline_list, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> headlines = ...; // populate headlines list
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.headline_list, R.id.my_text, headlines));
}
}
Run Code Online (Sandbox Code Playgroud)

我目前正在开发一个应用程序,其中每个抽屉的文本旁边都有图标.我的目标是图标的布局如下所示:

我搜索过互联网,似乎无法找到任何直接使用Android Studio导航抽屉模板的示例.这是我的代码.
MainActivity.java:
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence …Run Code Online (Sandbox Code Playgroud) java android android-listfragment android-studio navigation-drawer
我一直在尝试创建一个从活动(GradeListActivity)打开listview片段(GradeListFragment)的应用程序.单击某个项目时,它会打开一个新片段(GradeDetailFragment).如果处于纵向模式,它将替换fragment1中的列表片段.如果在横向中,它将替换空片段2.我的问题是当我切换方向时应用程序崩溃.从两个角度切换都会产生相同的错误.
04-09 12:10:52.240: E/AndroidRuntime(2221): FATAL EXCEPTION: main
04-09 12:10:52.240: E/AndroidRuntime(2221): Process: bcs421.christophergoepfert.hwk.gradeapp.presentation, PID: 2221
04-09 12:10:52.240: E/AndroidRuntime(2221): java.lang.RuntimeException: Unable to start activity ComponentInfo{bcs421.christophergoepfert.hwk.gradeapp.presentation/bcs421.christ ophergoepfert.hwk.gradeapp.presentation.GradeListActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.app.ActivityThread.access$900(ActivityThread.java:144)
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.os.Looper.loop(Looper.java:135)
04-09 12:10:52.240: E/AndroidRuntime(2221): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-09 12:10:52.240: E/AndroidRuntime(2221): at java.lang.reflect.Method.invoke(Native Method) …Run Code Online (Sandbox Code Playgroud) android illegalstateexception android-fragments android-listfragment