ListFragment无法滚动

时间:2016-09-05 20:53:37

标签: android listview

我是Android编程的初学者,也是初学英语的人,所以首先我要说对不起我的英语可能很难理解

我尝试在扩展listfragment的片段中添加listview。我相信我可以在没有额外设置的情况下滚动列表视图,但不知何故列表视图似乎已修复且无法滚动

这是fragmentpage_layout.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="wrap_content"
    android:background="#949497">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ffffff"
        android:divider="#949497"
        android:dividerHeight="5dp"
        android:drawSelectorOnTop="false" />
</LinearLayout>

这是我的PageFragment扩展了ListFragment

import android.content.Intent;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

import android.widget.Toast;

import java.util.*;

public class PageFragment extends ListFragment{
    public static  final String ARGS_PAGE="args_page";
    public int mPage;

    private ArrayList<String> aString=new ArrayList<>();
    private ArrayList<String> urlString=new ArrayList<>();
    private ArrayList<String> images=new ArrayList<>();

    private ArrayList<MyNews> myNewses=new ArrayList<>();
    private MyAdapter myAdapter;



    public static PageFragment newInstance(int page,ArrayList<MyNews> myNewses){
        PageFragment fragment=new PageFragment();

        Bundle args=new Bundle();
        args.putSerializable(ARGS_PAGE, page);

        fragment.setArguments(args);
        fragment.myNewses=myNewses;

        return fragment;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage=getArguments().getInt(ARGS_PAGE);

        for(int i=0;i<=myNewses.size()-1;i++){
            aString.add(myNewses.get(i).getNewsTitle());
            urlString.add(myNewses.get(i).getSource_url());
            images.add(myNewses.get(i).getImages());
        }

    }

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

        View view=inflater.inflate(R.layout.fragment_page,container,false);


        myAdapter= new MyAdapter(inflater,aString,images);
        setListAdapter(myAdapter);

        return view;
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Toast.makeText(getActivity(), aString.get(position).toString(), Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(getActivity(),WebActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.putExtra("source_url",myNewses.get(position).getSource_url().toString());
        intent.putExtra("image",myNewses.get(position).getImages().toString());
        intent.putExtra("title",myNewses.get(position).getNewsTitle().toString());
        intent.putExtra("origin",myNewses.get(position).getOrigin().toString());
        intent.putExtra("news_id",myNewses.get(position).getNews_id().toString());
        intent.putExtra("category",myNewses.get(position).getCategory().toString());
        startActivity(intent);
    }
}

最后是我的listview适配器

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import java.util.*;


public class MyAdapter extends BaseAdapter {

    private ArrayList<String> list = new ArrayList<>();
    private ArrayList<String> images_url=new ArrayList<>();
    private LayoutInflater inflater;

    public MyAdapter(LayoutInflater inflater, ArrayList<String> list,ArrayList<String> images){
        this.inflater = inflater;
        this.list = list;
        this.images_url=images;
    }

    public DisplayImageOptions getDisplayOption() {
        DisplayImageOptions options;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)              
                .considerExifParams(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
        return options;
    }

    @Override
    public int getCount(){
        return list.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder viewHolder;
        if(null == convertView){
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item, parent, false);

            viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
            viewHolder.imageView= (ImageView) convertView.findViewById(R.id.item_image);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(list.get(position).toString());
        if(images_url.get(position)=="NoImage"){
            viewHolder.imageView.setBackgroundResource(R.drawable.me);
        }
        if(MainActivity.canConn) {
            ImageLoader.getInstance().displayImage(images_url.get(position), viewHolder.imageView);
        }
        else{
            viewHolder.imageView.setBackgroundResource(R.drawable.me);
        }
        return convertView;
    }

    @Override
    public long getItemId(int position){
        return 0;
    }

    @Override
    public Object getItem(int position){
        return null;
    }

    class ViewHolder {
        TextView textView;
        ImageView imageView;
    }
}

因为无法滚动我的意思是我可以在我的电脑上的Android模拟器上滚动列表视图,但不能通过拖动鼠标滚动它以及用手指在手机上拖动它

0 个答案:

没有答案