为什么Listfragment滚动视图后的列表,我看到重复的元素? [英] Why Listfragment scroll the list behind the view and I see duplicate the elements?
问题描述
我正在尝试找到相同的问题,但没有发现任何类似的问题.我的问题是:
I'm trying to find same problem but I don't found anything similar. My problem is:
我有一个listFragment,它从sqlite获取数据.一切正常,但是当listview具有更多元素并且我需要滚动查看更多元素时,该视图具有罕见的行为.我可以在视图中看到所有元素都是静态的,但是在视图的后面,所有元素都在滚动.
I have a listFragment that get the data from the sqlite. All works fine but when the listview have more elements and I need to do a scroll to see more elements, the view have a rare behaviour. I can see in the view how all the elements are static but in the behind of the view all the elements are doing scroll.
我添加图片以更好地查看行为.
I add a picture to see better the behaviour.
这是我的listFragment类:
This is my 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:
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>
这是我创建fragmentList的地方:
This is where I create the fragmentList:
@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();
}
listview xml:
listview 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
是重复的.
I have to change my answer. I don't think the ListView
is duplicated. Either version of the fragment creation code should work fine. I think the problem is that your Fragment
is duplicated.
我对listview.xml的浏览不够仔细.您已经在listview.xml中声明了FavouritesActionBar
片段,但是在代码中创建了另一个片段,并试图替换声明的片段.这是多余的.
I didn't look carefully enough at listview.xml. You already declared a FavouritesActionBar
fragment in listview.xml, yet in your code you create another one and are trying to replace the declared one. This is redundant.
我记得看到一些评论,说明将XML声明的片段与编码的片段混在一起是不好的.我找不到关于它的引用,但是我可以告诉你,每次我尝试做类似您正在做的事情时,我都会遇到麻烦.现在,我总是只用XML声明空容器ViewGroup
,并在代码中对其进行分段事务.
I remember seeing some comments about how it's not good to mix XML-declared fragments with coded fragments. I can't find the reference to that, but I can tell you that every time I've tried to do something like what you're doing, I've gotten into trouble. Now I always just declare empty container ViewGroup
s in XML and do fragment transactions on them in the code.
您有两种选择:
-
使用编码后的片段并将您的listview.xml更改为此:
Use the coded fragment and change your listview.xml to this:
<?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);
}
这篇关于为什么Listfragment滚动视图后的列表,我看到重复的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!