Android ListView向下滚动,但拒绝向上滚动
我用导航抽屉。 在主活动中,我加载了一个片段,其布局在LinearLayout中包含一个ListView。 一切正常。除此之外,listview内容可以平滑地向下滚动,但不能再次向上滚动到顶部。它只允许在用力向下弹时到达顶部。 下面是列表项的列表视图布局和行布局 news_feed.xmlAndroid ListView向下滚动,但拒绝向上滚动,android,listview,scroll,ion,Android,Listview,Scroll,Ion,我用导航抽屉。 在主活动中,我加载了一个片段,其布局在LinearLayout中包含一个ListView。 一切正常。除此之外,listview内容可以平滑地向下滚动,但不能再次向上滚动到顶部。它只允许在用力向下弹时到达顶部。 下面是列表项的列表视图布局和行布局 news_feed.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/
<?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 "?";
}
}
公共类NewsFeedFragment扩展了片段{
私有mysimplearlayadapter myAdapter;
私有列表视图myListView;
私人语境;
私有静态最终字符串标记=“NewsFeedFragement”;
公共新闻提要片段(){
//片段子类需要空构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.news\u提要,容器,false);
myListView=(ListView)rootView.findViewById(R.id.newsFeed\u ListView);
getActivity().setTitle(“主页”);
返回rootView;
}
@凌驾
public void onStart(){
super.onStart();
//super.onActivityCreated(savedInstanceState);
myContext=getActivity().getApplicationContext();
字符串rssURL=”http://feeds.feedburner.com/TechCrunch/";
带(myContext)的离子
.负载(rssURL)
.asString()
.withResponse()
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(异常e,响应xmlResponse){
if(xmlResponse.getHeaders().code()==200){
字符串xmlString=xmlResponse.getResult();
//字符串[]值;
ArrayList)用于获取XML文档和将图像URL加载到ImageView中
任何帮助都将不胜感激。
谢谢。作为提示,您可以使用asDocument获取一个XML文档,这样您就不需要自己从字符串中解析它。至于您的问题,我猜有什么东西正在吞噬您的触摸事件。要详细说明我的问题,请在我尝试向上滚动(向下滑动列表视图)的同时,内容被推回底部或接近底部。@koush感谢使用asDocumentOk的提示。我找到了答案。问题是我没有在row\u layout.xml中为imageView设置固定高度。我所要做的就是将android:layout\u height=“wrap\u content”更改为类似android:layout\u height=“50dp”的内容我们可以走了。吸取了教训。有人请把这个问题标记为已解决。[我不能这样做,可能是因为我是新来的?]
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;
}
}