0

I use a navigation drawer. In the main activity, I load a fragment with a layout containing a ListView inside a LinearLayout. Everything works fine. Except, the listview content can be scrolled down smoothly, but cannot be scrolled up to the top again. It only allows to get to the top when forcefully flicked down. Here is my listview layout and row layout for the list items.

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>

This is my fragment class

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 "?";
    }
}

This is my array adapter class

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;
    }
}

As you can see, I am using Ion library (https://github.com/koush/ion) for both getting the XML document and loading the image urls into the imageViews.

Any help would be appreciated. Thanks.

chenzen
  • 63
  • 1
  • 1
  • 7
  • as a tip, you can use asDocument, and get an XML document, so you dont need to parse it yourself from a string. – koush May 05 '15 at 02:56
  • as for your question, i am guessing something is eating your touch events. – koush May 05 '15 at 02:57
  • to elaborate on my question, while i try to scroll up (swipe down the listview), the content is pushed back to the bottom or near the bottom. – chenzen May 05 '15 at 11:27
  • @koush thanks for the tip to use asDocument – chenzen May 05 '15 at 13:31
  • Ok. I found the answer. The problem is that I have not set a fixed height for the imageView in row_layout.xml. All I have to do is change android:layout_height="wrap_content" to something like android:layout_height="50dp" and we are good to go. Lesson learned. Somebody please mark this question solved. [i can't do it probably because I am new here?] – chenzen May 05 '15 at 14:12

0 Answers0