查看带有arraylist对象的寻呼机循环

时间:2015-10-08 09:43:48

标签: android arraylist

我有一个问题,在我的应用程序中我有一个视图寻呼机(在视图寻呼机上有3个项目的arraylist A显示3页),我使用了视图寻呼机循环,我跟随:http://social.technet.microsoft.com/wiki/contents/articles/1104.troubleshoot-and-optimize-queries-with-azure-sql-database.aspx 现在我有100个项目的arraylist B,我希望在浏览器循环中显示arraylist B中的项目值。

我怎么做?

package com.creativei.viewpagerloop;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
	private static final String LOG_TAG = "ViewPagerLoop";

	static ArrayList<data> arraydt = new ArrayList<data>();
	ArrayList<data> str = new ArrayList<data>();

	int Cur = 1;
	static int lastPage = 1;
	Button btnUp, btnDown;
	static int nextId;
	int id = 2;
	private ViewPager pager;

	private ArrayList<data> arraydt1= new ArrayList<data>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		str.add(new data("0", "hello0"));
		str.add(new data("1", "hello1"));
		str.add(new data("2", "hello2"));
		str.add(new data("3", "hello3"));
		str.add(new data("4", "hello4"));
		str.add(new data("5", "hello5"));
		str.add(new data("6", "hello6"));
		str.add(new data("7", "hello7"));
		str.add(new data("8", "hello8"));
		str.add(new data("9", "hello9"));

		arraydt.add(new data("1", "hello0"));
		arraydt.add(new data("2", "hello1"));
		arraydt.add(new data("3", "hello2"));
		

		pager = (ViewPager) findViewById(R.id.pager);
		TextView counter = (TextView) findViewById(R.id.counter);

		SimpleViewPagerAdapter adapter = new SimpleViewPagerAdapter(this,
				getSupportFragmentManager(), pager, counter, arraydt);
		pager.setAdapter(adapter);
		pager.setOnPageChangeListener(adapter);
		pager.setCurrentItem(1, false);

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				if (position == 0) {
					pager.setCurrentItem(arraydt.size(), false);

				} else if (position == arraydt.size() + 1) {
					pager.setCurrentItem(1, false);
					// counter.setText("1");
				}
				//

				Log.d("POSITION", "" + position);
				Log.d("Id hien tai", "" + nextId);
				Log.d("ID", "" + id);

				// if (nextId == 0)
				if (nextId == 1) {

					if (position == 2) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+12 ", "" + id);
						Log.d("Id", str.get(id).getText());
						 arraydt.set(1, str.get(id));

					} else if (position == 3) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
						Log.d("-13 ", "" + id);
						Log.d("Id", str.get(id).getText());
						 arraydt.set(2, str.get(id));

					}
					// nextId = position;

				} else if (nextId == 2) {
					if (position == 3) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+23 ", "" + id);
						Log.d("Id", str.get(id).getText());
						 arraydt.set(0, str.get(id));
					} else if (position == 1) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
						Log.d("-21 ", "" + id);
						Log.d("Id", str.get(id).getText());
						 arraydt.set(2, str.get(id));

					}
					// nextId = position;

				} else if (nextId == 3) {
					if (position == 1) {
						id++;
						if (id == str.size()) {
							id = 0;
						}

						Log.d("+31 ", "" + id);
						Log.d("Id", str.get(id).getText());
						arraydt.set(1, str.get(id));

					} else if (position == 2) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
						Log.d("-32 ", "" + id);
						Log.d("Id", str.get(id).getText());
						 arraydt.set(0, str.get(id));

					}
					// nextId = position;
				} else if (nextId == 0) {
					Toast.makeText(getApplication(), "000 " + nextId,
							Toast.LENGTH_SHORT).show();
				} else if (nextId == 4) {
					Toast.makeText(getApplication(), "444 " + nextId,
							Toast.LENGTH_SHORT).show();
				}
				nextId = pager.getCurrentItem();

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public static class SimpleFragment extends Fragment {
		private int position;

		public SimpleFragment() {
			// this.position=postiton;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			// String content1 = getArguments().getString("content");
			int id1 = getArguments().getInt("position");
			// int id = Integer.parseInt(content1);
			// Log.d("Content", content1);
			LinearLayout llayout = (LinearLayout) rootView
					.findViewById(R.id.Llayout);
			TextView tv = new TextView(getActivity());

			tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));
			tv.setText(arraydt.get(id1).getText().toString()+" "+id1);
			llayout.addView(tv);
			return rootView;
		}
	}

	public static class SimpleViewPagerAdapter extends
			FragmentStatePagerAdapter implements OnPageChangeListener {
		private ArrayList<data> arraydt;
		private String[] content;
		private ViewPager pager;
		private TextView counter;
		private Context context;

		public SimpleViewPagerAdapter(Context context, FragmentManager fm,
				ViewPager pager, TextView counter, ArrayList<data> arraydt) {

			super(fm);
			this.pager = pager;
			this.counter = counter;
			this.arraydt = arraydt;
			this.context = context;

		}

		@Override
		public Fragment getItem(int position) {
			SimpleFragment fragment = new SimpleFragment();

			Bundle bundle = new Bundle();
			int index = position - 1;
			if (position == 0) {
				index = arraydt.size() - 1;
			} else if (position == arraydt.size() + 1) {
				index = 0;
			}
			Log.d(LOG_TAG, "For page at position " + position
					+ ",fetching item at index " + index);
			bundle.putInt("position", index);
			fragment.setArguments(bundle);

			return fragment;
		}

		@Override
		public int getCount() {
			return arraydt.size() + 2;
		}

		@Override
		public void onPageSelected(int position) {
			Log.d("nextId", "" + position);
			nextId = position;

		}

		private String makeCounterText(int pageNo) {
			return "Page " + pageNo + " of " + arraydt.size();
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			Toast.makeText(context, "arg0: " + arg0, Toast.LENGTH_LONG).show();
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

	}
}
main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.creativei.viewpagerloop.MainActivity"
    tools:ignore="MergeRootFrame" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/counter" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal" />

</RelativeLayout>
fragment.xml之

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.creativei.viewpagerloop.SimpleFragment" >

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/Llayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以使用相同的机制,但更改某些逻辑以动态加载数据,这是我的建议:

假设您的ArrayList不应超过5个元素(不占用太多内存)。

您第一次只能加载5个元素:B99-B100-B1-B2-B3

当前页面为B1,您已经有下一页和前两页(在切换时准备数据)。

  • 如果您切换到右侧(B1-> B2):删除B99并加载B4(在后台),这样您的列表就会变为B100-B1-B2-B3-B4

  • 如果切换到leftt(B1-> B100):删除B3并加载B98(在后台),这样您的列表就会变为B98-B99-B100-B1-B2

请注意,更新列表后,当前/可见页面的位置将发生变化。你必须处理这个问题。