我有一个使用导航抽屉和几个片段运行的应用程序。 一切正常但是这个,FragHome,我有一个图像和ListView。 片段的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_home_copia, container, false);
mStickyView = (TextView) rootView.findViewById(R.id.sticky);
mStickyView.setText(R.string.sticky_item);
mListView = (ListView) rootView.findViewById(R.id.listView);
mItemTop = rootView.findViewById(R.id.itemTop);
LayoutInflater inflater1 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater1.inflate(R.layout.top_layout, null);
mPlaceholderView = v.findViewById(R.id.placeholder);
mListView.addHeaderView(v);
mListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
onScrollChanged();
ViewTreeObserver obs = mListView.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
onScrollChanged();
}
});
// Create and set the adapter for the listView.
SimpleAdapter simpleAdpt = new SimpleAdapter(getActivity(), createListViewData(), R.layout.list_item, new String[] {"item"}, new int[] {android.R.id.text1});
mListView.setAdapter(simpleAdpt);
return rootView;
}
private void onScrollChanged() {
View v = mListView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// This check is needed because when the first element reaches the top of the window, the top values from top are not longer valid.
if (mListView.getFirstVisiblePosition() == 0) {
mStickyView.setTranslationY(
Math.max(0, mPlaceholderView.getTop() + top));
// Set the image to scroll half of the amount scrolled in the ListView.
mItemTop.setTranslationY(top / 2);
}
}
/**
* Function used to create the HashMap needed for ListView item using Simple Adapter.
* @param key
* @param name
* @return
*/
private HashMap<String, String> createItem(String key, String name) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(key, name);
return item;
}
private List<Map<String, String>> createListViewData() {
List<Map<String, String>> itemList = new ArrayList<Map<String,String>>();
itemList.add(createItem("item","Something");
return itemList;
}
这是布局
<!--suppress ALL -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/itemTop"
android:background="@drawable/home_background"
style="@style/Item.Top" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:scrollbars="@null"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@+id/sticky"
android:clickable="true"
android:focusable="true"
style="@style/Item.Sticky" />
在styles.xml中我有两个,Item.Top和Item.Sticky: TOP:
<style name="Item.Top">
<item name="android:background">#FFFFFF</item>
<item name="android:scaleType">fitCenter</item>
</style>
粘性:
<style name="Item.Sticky">
<item name="android:background">#FFFFFF</item>
<item name="android:layout_height">@dimen/sticky_height</item>
<item name="android:gravity">center</item>
<item name="android:textAppearance">?android:textAppearanceLarge</item>
<item name="android:textAllCaps">true</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textColor">#09c</item>
</style>
这是完整的logcat跟踪:
05-12 18:01:08.280 8044-8044/it.radonmap.navdrawertutorial E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: it.radonmap.navdrawertutorial, PID: 8044
android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2387)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17473)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17473)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17473)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:17473)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17473)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17473)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17473)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17473)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2622)
at android.view.View.measure(View.java:17473)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1192)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1399)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5845)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at an
drawable文件夹中的home_background是600x400分辨率的图像。 我试图将它减少到300x200,我发现这个解决方法有点搜索,但没有任何改变。
任何帮助将不胜感激。提前谢谢!
编辑1:layout_top.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="@+id/topTransparent"
style="@style/Item.TopTransparent" />
<View
android:id="@+id/placeholder"
android:layout_width="match_parent"
android:layout_height="@dimen/sticky_height" />
</LinearLayout>