Android ListFragment有永久加载栏

时间:2017-06-20 19:12:25

标签: android android-fragments

我是Android开发的新手,我最近的一个项目遇到了问题。我有一个listfragment作为一个活动的一部分,它工作并加载我放入它的测试信息,但是,有一个加载微调器,无论我尝试什么都不会消失。这是图像: Perpetual Loading Bar。 这是我的代码:

MainActivity.java

package com.projects.nick.pheme;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FragmentManager fragmanager = getSupportFragmentManager();
    FragmentTransaction fragtrans = fragmanager.beginTransaction();
    setContentView(R.layout.activity_main);
    //setting the home screen
    FragmentList listFrag = new FragmentList();
    fragtrans.add(R.id.FirstFragment, listFrag);
    fragtrans.commit();
}

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@color/primary_material_light"
        android:orientation="horizontal"
        android:weightSum="3"
        android:id="@+id/linearLayout">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/hometestdonotuse"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/hometestdonotuse"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/hometestdonotuse"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_weight="1"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/linearLayout"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <fragment
            android:id="@+id/FirstFragment"
            android:name="android.support.v4.app.ListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

FragmentList.java

package com.projects.nick.pheme;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;


public class FragmentList extends ListFragment{

private ArrayList<NewsCard> cardArray = new ArrayList<NewsCard>();

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.list_fragment, container, false);
    ListView listView = (ListView) view.findViewById(android.R.id.list);

    Context context = getActivity().getApplicationContext();
    //TODO get rid of this and have the actual news articles show up
    NewsCard testCard = new NewsCard("Nick invents the best app ever", "Nick's Mom");
    NewsCard secondCard = new NewsCard("This is a test", "Nicks dad");
    cardArray.add(0,testCard);
    cardArray.add(1,secondCard);
    MainAdapter adapter = new MainAdapter(context, cardArray);
    listView.setAdapter(adapter);

    return view;

}

}

list_fragment.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"
    android:weightSum="1">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>  

MainAdapter.java

package com.projects.nick.pheme;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class MainAdapter extends ArrayAdapter<NewsCard> {

public MainAdapter(Context context, ArrayList<NewsCard> elements) {
    super(context, 0, elements);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //get the data
    NewsCard card = getItem(position);
    // Check if view is being reused, otherwise inflate
    if (convertView == null) {
        convertView = 
LayoutInflater.from(getContext()).inflate(R.layout.card_list, parent, false);
    }
    TextView cardSource = (TextView) 
convertView.findViewById(R.id.sourceArticle);
    TextView cardTitle = (TextView) 
convertView.findViewById(R.id.titleArticle);
    // populate the data into the template view using the data object
    cardSource.setText(card.getSource());
    cardTitle.setText(card.getTitle());

    return convertView;
}


}

NewsCard类只是我用来保存信息的基本类,它只有一个构造函数,并且有两个用于字符串参数的访问器方法。

如果我遗失某些东西或某些不清楚的地方,请告诉我。我是stackoverflow的新手。谢谢!

1 个答案:

答案 0 :(得分:0)

来自documentation:

  

您必须使用ListFragment.setListAdapter()将列表与适配器相关联。不要直接调用ListView.setAdapter(),否则将跳过重要的初始化。

FragmentList而不是致电:

listView.setAdapter(adapter);

呼叫

setListAdapter(adapter);

编辑: 另一个问题(我认为)是添加两个FragmentList的实例。

而不是

<fragment
            android:id="@+id/FirstFragment"
            android:name="android.support.v4.app.ListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

使用:

<FrameLayout
            android:id="@+id/FirstFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />