为什么 Listfragment 在视图后面滚动列表并且我看到重复的元素?
Why Listfragment scroll the list behind the view and I see duplicate the elements?
我试图找到同样的问题,但没有发现任何类似的问题。我的问题是:
我有一个从 sqlite 获取数据的 listFragment。一切正常,但是当列表视图有更多元素并且我需要滚动以查看更多元素时,该视图有一种罕见的行为。我可以在视图中看到所有元素是如何静态的,但在视图后面所有元素都在滚动。
我添加图片以更好地查看行为。
这是我的 listFragment class:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataBase = new DataBaseWrapper(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = null;
final String[] name;
final String[] url;
dataBase.getAllData();
name = new String[dataBase.getAllData().size()];
url = new String[dataBase.getAllData().size()];
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map;
for (int i = 0; i < dataBase.getAllData().size(); i++) {
name[i] = dataBase.getAllData().get(i).getName();
url[i] = dataBase.getAllData().get(i).getUrl();
map = new HashMap<String, String>();
map.put("name", name[i]);
map.put("url", url[i]);
list.add(map);
}
rootView = (ViewGroup) inflater.inflate(R.layout.fragment_listview, container, false);
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.favourites, new String[] { "name", "url" }, new int[] { R.id.newspaperNameFavourite, R.id.passUrl });
setListAdapter(adapter);
setRetainInstance(true);
return rootView;
}
Fragment_Listview xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/favourites_fragment">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_marginTop="50dp"/>
</FrameLayout>
收藏夹xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:padding="5dp">
<TextView
android:id="@+id/newspaperNameFavourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/passUrl"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="135dp"
android:layout_marginEnd="135dp"
android:visibility="gone"/>
</RelativeLayout>
这是我创建片段列表的地方:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
Fragment fr = new FavouritesActionBar();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
列表视图xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.exagon.goalnews.favourites.FavouritesActionBar"
android:id="@+id/the_actual_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
我必须更改我的答案。我不认为 ListView
是重复的。片段创建代码的任一版本都可以正常工作。我认为问题在于您的 Fragment
重复了。
我没仔细看listview.xml。您已经在 listview.xml 中声明了一个 FavouritesActionBar
片段,但在您的代码中您创建了另一个片段并试图替换已声明的片段。这是多余的。
我记得看到一些评论说将 XML 声明的片段与编码的片段混合是多么不好。我找不到相关的参考资料,但我可以告诉你,每次我尝试做你正在做的事情时,我都会遇到麻烦。现在我总是在 XML 中声明空容器 ViewGroup
s 并在代码中对它们进行片段交易。
您有两个选择:
使用编码片段并将您的 listview.xml 更改为:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_place"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
或
使用 XML 声明的片段并将您的 onCreate()
更改为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
}
我试图找到同样的问题,但没有发现任何类似的问题。我的问题是:
我有一个从 sqlite 获取数据的 listFragment。一切正常,但是当列表视图有更多元素并且我需要滚动以查看更多元素时,该视图有一种罕见的行为。我可以在视图中看到所有元素是如何静态的,但在视图后面所有元素都在滚动。
我添加图片以更好地查看行为。
这是我的 listFragment class:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataBase = new DataBaseWrapper(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = null;
final String[] name;
final String[] url;
dataBase.getAllData();
name = new String[dataBase.getAllData().size()];
url = new String[dataBase.getAllData().size()];
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map;
for (int i = 0; i < dataBase.getAllData().size(); i++) {
name[i] = dataBase.getAllData().get(i).getName();
url[i] = dataBase.getAllData().get(i).getUrl();
map = new HashMap<String, String>();
map.put("name", name[i]);
map.put("url", url[i]);
list.add(map);
}
rootView = (ViewGroup) inflater.inflate(R.layout.fragment_listview, container, false);
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.favourites, new String[] { "name", "url" }, new int[] { R.id.newspaperNameFavourite, R.id.passUrl });
setListAdapter(adapter);
setRetainInstance(true);
return rootView;
}
Fragment_Listview xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/favourites_fragment">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_marginTop="50dp"/>
</FrameLayout>
收藏夹xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:padding="5dp">
<TextView
android:id="@+id/newspaperNameFavourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/passUrl"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="135dp"
android:layout_marginEnd="135dp"
android:visibility="gone"/>
</RelativeLayout>
这是我创建片段列表的地方:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
Fragment fr = new FavouritesActionBar();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
列表视图xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.exagon.goalnews.favourites.FavouritesActionBar"
android:id="@+id/the_actual_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
我必须更改我的答案。我不认为 ListView
是重复的。片段创建代码的任一版本都可以正常工作。我认为问题在于您的 Fragment
重复了。
我没仔细看listview.xml。您已经在 listview.xml 中声明了一个 FavouritesActionBar
片段,但在您的代码中您创建了另一个片段并试图替换已声明的片段。这是多余的。
我记得看到一些评论说将 XML 声明的片段与编码的片段混合是多么不好。我找不到相关的参考资料,但我可以告诉你,每次我尝试做你正在做的事情时,我都会遇到麻烦。现在我总是在 XML 中声明空容器 ViewGroup
s 并在代码中对它们进行片段交易。
您有两个选择:
使用编码片段并将您的 listview.xml 更改为:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_place" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/>
或
使用 XML 声明的片段并将您的
onCreate()
更改为:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); }