滑动详细信息视图

时间:2014-02-18 16:22:41

标签: android

我有ListView项目。我正在使用XML来解析数据。在那一刻,我解析了DetailsView中一个项目的数据。当我点击项目时,如何使用SwipeViews项目之间的项目进行DetailsView?如何使用ViewPager实现?我尝试开发一些例子,但没有用。如果有人帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

首先为您的详细信息创建Fragment。这是一个细节。你要刷的细节。因此,请将您的布局应用于此。

@SuppressLint("ValidFragment")
public class Detail extends Fragment {


     private final String detail;


        public DetailView(String detail) {
            this.detail = detail;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detailfragment, container, false);

        TextView textView = (TextView) view.findViewById(R.id.detailText);
        textView.setText(detail);

        return view;
    }

}

现在创建您的Activity。这是您单击时要去的Activity。现在,代码为每个细节添加Fragment

public class Main extends FragmentActivity {

 public static ViewPager mPager;
 private MyAdapter mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mPager = (ViewPager) findViewById(R.id.viewPager1);

    mAdapter = new MyAdapter(getSupportFragmentManager());
    mPager.setAdapter(mAdapter);
}

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public int getCount() {
    //The count of details. This will return 5 swipeable views.
        return 5;
    }

    @Override
    public Fragment getItem(int position) {

             return new Detail("I'm a detail!");

              }
}


}

这将创建5个细节,文字“我是一个细节!”。现在,我假设您正在使用存储详细信息的ArrayList。然后你可以得到这样的细节:

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm, ArrayList<HashMap<String, String>> d) {
        super(fm);
        data = d;
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public Fragment getItem(int position) {
     HashMap<String, String> map = new HashMap<String, String>();
     map = data.get(position);
     Log.d("", data.get(position).toString());

             return new Detail(map.get('KeyForYourDetail'));


              }
}

同样在MainActivity更改

mAdapter = new MyAdapter(getSupportFragmentManager());

到这个

mAdapter = new MyAdapter(getSupportFragmentManager(), yourArrayList);

修改

让我们从头开始。首先,您的MainActivity包含ListView。在ListView中,您正在调用ArrayList中的项目。单击某个项目时,可以从(?)获取有关该项目的详细信息。现在我的上面的代码不能这样做。你必须先做自己的事。获得详细信息后,只需替换下面的title部分即可。总而言之,如果您可以获得“详细信息”,我发布的代码应该有效,如果您不知道如何获取详细信息,那么您应该创建另一个问题。

return new Detail(title);

编辑2

好的,这是另一部分。下面的代码是FragmentActivity。

首先,我们需要声明我们的适配器和ViewPager

public static ViewPager mPager;
private MyAdapter mAdapter;
ArrayList<HashMap<String, String>> detailList= new ArrayList<HashMap<String, String>>();

MyAdapter是将详细信息放入ViewPager的适配器。要保留详细信息,我们还需要ArrayList,如上所述。现在,你说你没有解析问题。所以我正在跳过这一部分。在您解析时,我假设您正在使用NodeList(我从here获取该部分)

NodeList nl = doc.getElementsByTagName(KEY_ITEM);

//循环遍历所有项目节点
for(int i = 0; i&lt; nl.getLength(); i ++){     String name = parser.getValue(e,KEY_NAME); //命名子值     String cost = parser.getValue(e,KEY_COST); //花费儿童价值     字符串描述= parser.getValue(e,KEY_DESC); //描述子值 }

在for循环中,创建一个HashMap这样的

 HashMap<String, String> map = new HashMap<String, String>();

在我们创建ArrayList时,请记住吗?它的对象是HashMap。现在我们将填充我们的数据进行映射,然后将map添加到ArrayList。 (再次,这在for循环中)

 map.put(TAG_DETAIL, detail);
 detailList.add(map);

现在这部分结束了,只剩下设置适配器了。

 mAdapter = new MyAdapter(getSupportFragmentManager(), detailList);
 mPager.setAdapter(mAdapter);

我们完成了!让我解释接下来会发生什么。你还记得我们的适配器吗?

 public MyAdapter(FragmentManager fm, ArrayList<HashMap<String, String>> d) {
        super(fm);
        data = d;
    }

此处如您所见,其内部需要FragmentMangerArrayList Hashmap。我们设置了getSupportFragmentManager()detailList。 Adapter获取detailList及其中的详细信息,然后使用它创建Fragments。我希望它足够清楚。如果不是,请再次询问。

答案 1 :(得分:0)

这是MyAdapter.java的代码:

公共类MyAdapter扩展了ArrayAdapter {

ImageLoader imageLoader;
DisplayImageOptions options;


public MyAdapter(Context ctx, int textViewResourceId, List<NewsFeed> sites) {
    super(ctx, textViewResourceId, sites);

    //Setup the ImageLoader, we'll use this to display our images
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx).build();
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);

    //Setup options for ImageLoader so it will handle caching for us.
    options = new DisplayImageOptions.Builder()
    .cacheInMemory()
    .cacheOnDisc()
    .build();
}


/*
 * (non-Javadoc)
 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
 * 
 * This method is responsible for creating row views out of a StackSite object that can be put
 * into our ListView
 */
@Override
public View getView(int pos, View convertView, ViewGroup parent){
    RelativeLayout row = (RelativeLayout)convertView;
    Log.i("Feed", "getView pos = " + pos);

// if(null == row){ // //没有回收的View,我们必须给一个人充气。 // LayoutInflater inflater =(LayoutInflater)parent.getContext()。getSystemService(Context.LAYOUT_INFLATER_SERVICE); // row =(RelativeLayout)inflater.inflate(R.layout.list_item,null); //}

    if (pos != 0){
        LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = (RelativeLayout)inflater.inflate(R.layout.list_item, null);
    }
    else
    {
        //No recycled View, we have to inflate one.
        LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = (RelativeLayout)inflater.inflate(R.layout.list_first_item, null);
    }


    //Get our View References
    final ImageView iconImg = (ImageView)row.findViewById(R.id.iconImg);
    TextView titleTxt = (TextView)row.findViewById(R.id.titleTxt);
    final ProgressBar indicator = (ProgressBar)row.findViewById(R.id.progress);

    //Initially we want the progress indicator visible, and the image invisible
    indicator.setVisibility(View.VISIBLE);
    iconImg.setVisibility(View.INVISIBLE);

    //Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
    ImageLoadingListener listener = new ImageLoadingListener(){



        @Override
        public void onLoadingStarted(String arg0, View arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
            indicator.setVisibility(View.INVISIBLE);
            iconImg.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
            // TODO Auto-generated method stub

        }

    };

    //Load the image and use our options so caching is handled.
    imageLoader.displayImage(getItem(pos).getImgUrl(), iconImg,options, listener);

    //Set the relavent text in our TextViews
    titleTxt.setText(getItem(pos).getTitle());

    return row;


}

}