android 페이지 목록 동적으로 처리하기


ButtonUtil.java

동적으로 페이지 목록 생성 메서드

import android.content.*;
import android.graphics.*;
import android.view.*;
import android.widget.*;

import com.cheil.gscm.*;

public class ButtonUtil {
	
	/**
	 * 페이징 처리를 위한 버튼을 설정한다.
	 */
	public static void setPageButton(int selectNum, int pageCount, int pageLine, 
			Context context, LinearLayout layout, View.OnClickListener onClickListener){
		layout.removeAllViews();
		
		if(selectNum == 0){
			return;
		}
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        		android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		
		params.setMargins(10, 0, 10, 0);
		
		int startPagePlus = 0;
		if(pageLine == 0){
			startPagePlus = 1;
		}else if(pageLine == 1){
			startPagePlus = 11;
		}else if(pageLine == 2){
			startPagePlus = 21;
		}else if(pageLine == 3){
			startPagePlus = 31;
		}else if(pageLine == 4){
			startPagePlus = 41;
		}else if(pageLine == 5){
			startPagePlus = 51;
		}
		
		if(startPagePlus+10 < pageCount){
			pageCount = startPagePlus+10;
		}
		
		for(int i=startPagePlus; i <= pageCount; i++){
			TextView bt = new TextView(context);
			bt.setTag("page_" + Integer.toString((i)));
			bt.setGravity(Gravity.CENTER);
			bt.setPadding(0, 0, 0, Utilities.getDpToPixel(context	, 4));
			bt.setWidth(Utilities.getDpToPixel(context, 54.9f));
			bt.setHeight(Utilities.getDpToPixel(context, 27));
			bt.setTextSize(Utilities.getDpToPixel(context, 10));
			
			if(selectNum == i){
//				bt.setBackgroundResource(R.drawable.bu_bottom_on);
				bt.setBackgroundResource(R.drawable.bottom_bg_num);
				bt.setTextColor(Color.BLACK);
			}else{
				bt.setBackgroundResource(android.R.color.transparent);
				bt.setTextColor(Color.WHITE);
			}
			
			bt.setText(Integer.toString((i)));
			bt.setOnClickListener(onClickListener);
			layout.addView(bt, params);
		}
	}
}


xxx.xml

...

    <LinearLayout

        android:id="@+id/ll_news_navigation_page"

        android:layout_width="fill_parent"

        android:layout_height="48.6dp"

        android:background="@drawable/bottom_bg"

        android:layout_marginTop="-6.3dp"

        android:gravity="center" >


        <Button

            android:id="@+id/btn_news_page_left"

            android:layout_width="0dp"

            android:layout_height="36.0dp"

            android:layout_weight="4"

            android:layout_marginLeft="36.0dp"

            android:layout_marginTop="0dp"

            android:background="@drawable/bu_arrow_left_selector" />


        <LinearLayout

            android:id="@+id/ll_news_page"

            android:layout_width="0dp"

            android:layout_height="36.0dp"

            android:layout_weight="60"

            android:layout_marginLeft="9.0dp"

            android:layout_marginRight="9.0dp"

            android:layout_marginTop="0dp"

            android:layout_marginBottom="1dp"

            android:paddingTop="2.5dp"

            android:paddingBottom="2.5dp"

            android:gravity="center"

            android:orientation="horizontal" >

        </LinearLayout>


        <Button

            android:id="@+id/btn_news_page_right"

            android:layout_width="0dp"

            android:layout_height="36.0dp"

            android:layout_weight="4"

            android:layout_marginRight="36.0dp"

            android:layout_marginTop="0dp"

            android:background="@drawable/bu_arrow_right_selector" />

    </LinearLayout>

...


MainActivity.java

...
public class MainActivity extends Activity {

	private Context mContext;
	private Activity mActivity;
	private LinearLayout mLl_Page = null;
	private LinearLayout mLl_NavigationPage = null;
	private int mPageNum = 1; // 선택한 페이지
	private int mPageCount= 0; // 전체 페이지 수
	private int mPageSelectLine= 0; // 선택한 페이지 라인
	private int mNewsDataCount = 0; // 아이템 전체 수
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = this;
		mActivity = this;
		
		mLl_Page = (LinearLayout) findViewById(R.id.ll_news_page);
		mLl_NavigationPage = (LinearLayout) findViewById(R.id.ll_news_navigation_page);

		((Button)findViewById(R.id.btn_news_page_left)).setOnClickListener(mOnClickListener);
		((Button)findViewById(R.id.btn_news_page_right)).setOnClickListener(mOnClickListener);
	}
	View.OnClickListener mOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			int id = v.getId();
			switch(id){
			case R.id.btn_news_page_left: // Page Left
			{
				if(mPageCount > 0){
					if(mPageSelectLine == 0){
						break;
					}
					mPageSelectLine--;
					ButtonUtil.setPageButton(mPageNum, mPageCount, mPageSelectLine, mActivity, mLl_Page, mOnClickListener);
				}
				break;
			}
			case R.id.btn_news_page_right: // Page Right
			{
				if(mPageCount > 0){
					if(mPageSelectLine*10+10 > mPageCount){
						break;
					}
					mPageSelectLine++;
					ButtonUtil.setPageButton(mPageNum, mPageCount, mPageSelectLine, mActivity, mLl_Page, mOnClickListener);
				}
				break;
			}
		}
			if (v.getTag() != null) {
				String vTag = v.getTag().toString();

				Log.d(TAG, "onClick:vTag = " + vTag);
				if (vTag.indexOf("page_") > -1) {
					mPageNum = Integer.parseInt(vTag.substring(vTag.indexOf("_") + 1, vTag.length()));
				}
			}
		}
	};
...



   //


+ Recent posts