0

我使用导航抽屉。在主要活动中,我加载了一个片段,其布局包含一个线性布局内的 ListView。一切正常。除此之外,listview 内容可以平滑向下滚动,但不能再次向上滚动到顶部。它只允许在用力向下轻弹时到达顶部。这是列表项的列表视图布局和行布局。

news_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:weightSum="1">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background"
        android:id="@+id/newsFeed_listView" />
</LinearLayout>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20sp" >
    </TextView>

</LinearLayout>

这是我的片段类

public class NewsFeedFragment extends Fragment {
    private MySimpleArrayAdapter myAdapter;
    private ListView myListView;
    private Context myContext;
    private static final String TAG = "NewsFeedFragement";
    public NewsFeedFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.news_feed, container, false);
        myListView = (ListView)rootView.findViewById(R.id.newsFeed_listView);

        getActivity().setTitle("Home");


        return rootView;
    }
    @Override
    public void onStart() {
        super.onStart();
        //super.onActivityCreated(savedInstanceState);

        myContext = getActivity().getApplicationContext();
        String rssURL = "http://feeds.feedburner.com/TechCrunch/";

        Ion.with(myContext)
                .load(rssURL)
                .asString()
                .withResponse()
                .setCallback(new FutureCallback<Response<String>>() {
                    @Override
                    public void onCompleted(Exception e, Response<String> xmlResponse) {

                        if (xmlResponse.getHeaders().code() == 200) {
                            String xmlString = xmlResponse.getResult();
                            //String[] values;
                            ArrayList<ArrayList<String>> list = new ArrayList<>();
                            try {
                                DocumentBuilderFactory dbf =
                                        DocumentBuilderFactory.newInstance();
                                DocumentBuilder db = dbf.newDocumentBuilder();
                                InputSource is = new InputSource();
                                is.setCharacterStream(new StringReader(xmlString));

                                Document doc = db.parse(is);
                                NodeList nodes = doc.getElementsByTagName("item");

                                // iterate the employees
                                for (int i = 0; i < nodes.getLength(); i++) {
                                    Element element = (Element) nodes.item(i);

                                    NodeList title = element.getElementsByTagName("title");
                                    Element titleElem = (Element) title.item(0);
                                    //System.out.println("Name: " + getCharacterDataFromElement(titleElem));

                                    NodeList thumbnail = element.getElementsByTagName("media:thumbnail");
                                    Element thumbnailElem = (Element) thumbnail.item(0);

                                    ArrayList<String> myItem = new ArrayList<String>();
                                    myItem.add(0, getCharacterDataFromElement(titleElem));
                                    myItem.add(1, thumbnailElem.getAttribute("url"));
                                    list.add(i, myItem);
                                }
                            }
                            catch (Exception e2) {
                                e2.printStackTrace();
                            }
                            if(list.size() > 0) {
                                myAdapter = new MySimpleArrayAdapter(myContext, list);
                                myListView.setAdapter(myAdapter);
                            }
                            else
                            {
                                Log.d(TAG, "No RSS items found");
                            }
                        }

                    }
                });
    }
    public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
        return "?";
    }
}

这是我的数组适配器类

public class MySimpleArrayAdapter extends ArrayAdapter<ArrayList<String>> {
    private final Context context;
    private final ArrayList<ArrayList<String>> values;

    public MySimpleArrayAdapter(Context context, ArrayList<ArrayList<String>> values) {
        super(context, R.layout.row_layout, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_layout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        //textView.setText(values[position]);
        String title = values.get(position).get(0);
        textView.setText(title);

        String imageURL = values.get(position).get(1);
        Ion.with(imageView)
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .load(imageURL);

        return rowView;
    }
}

如您所见,我使用 Ion 库 ( https://github.com/koush/ion ) 来获取 XML 文档并将图像 url 加载到 imageViews 中。

任何帮助,将不胜感激。谢谢。

4

0 回答 0