常用的代码块

// 执行线程,


private Thread mThread;

mThread = null;

if (mThread == null)

{

mThread = new Thread(){

public void run(){

super.run();

}

};

}


if (!mThread.isAlive())

{

mThread.start();

}


列表上拉加载数据

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:gravity="center"

   android:orientation="horizontal"

   >


<ProgressBar

android:id="@+id/pb_fpcf"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

style="\?android:attr/progressBarStyleSmall"

/>

<TextView

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   android:text="@string/tv_more_comment"

   android:textColor="@color/gray"

   android:textSize="12sp"

   />    


</LinearLayout >

//记录当前状态

private int pageIndex=2;

private int mScrollState;

private View mFooter;

mFooter.setVisibility(View.GONE);

//列表页脚

mFooter = LayoutInflater.from(mContext).inflate(

R.layout.fragment_post_comment_footer, null);

mFooter.setVisibility(View.GONE);

lvComments.addFooterView(mFooter);

//给ListView添加滚动监听器

lvComments.setOnScrollListener(commentsOnScrollL);

注册滚动条监听事件


OnScrollListener commentsOnScrollL = new OnScrollListener()

{


@Override

public void onScroll(AbsListView view, int firstVisibleItem,

int visibleItemCount, int totalItemCount)

{

//Log.v(TAG,

//"----------------------------------------------------------onScroll called!");

// TODO Auto-generated method stub

// 可视的最后一个列表项的索引

int lastVisibleItem = firstVisibleItem + visibleItemCount - 1;

//当列表正处于滑动状态且滑动到列表底部时,执行

if (mScrollState != AbsListView.OnScrollListener.SCROLL_STATE_IDLE

&& lastVisibleItem == totalItemCount - 1)

{// 执行线程 获取数据

mFooter.setVisibility(View.VISIBLE);

getData();

}


}


@Override

public void onScrollStateChanged(AbsListView view, int scrollState)

{

// TODO Auto-generated method stub

mScrollState = scrollState;

}

//下拉刷新

private ListViewHeaderPullRefresh listView;


adapter.notifyDataSetChanged();

listView.onRefreshComplete();


listView = (ListViewHeaderPullRefresh) view.findViewById(R.id.lv_al);

listView.setonRefreshListener(new OnRefreshListener()

{

public void onRefresh()

{

pb.setVisibility(View.VISIBLE);

new Thread()

{

public void run()

{

try

{

sleep(1000);


Message msg = new Message();

msg.what = 1;

mHandler.sendMessage(msg);


}

catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}


}

}.start();

}

});


};


你可能感兴趣的:(android,常用,代码块)