我正在使用具有動態視圖的ViewPager,如此link中所示。我將每個頁面添加到viewpager。該代碼創建的每個頁面,並把它添加到尋呼機適配器是在頁面的TextView如下兒童的OnClickListener防止Android ViewPager滑動
public View createPage()
{
TableLayout table = new TableLayout(mycontext);
TableRow row = new TableRow(mycontext);
for(int i=0; i <= num_of_views; i++)
{
TableLayout catTable = new TableLayout(mycontext);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = LayoutParams.MATCH_PARENT;
TableRow catName = new TableRow(mycontext);
catName.setGravity(0x03);
catName.setLayoutParams(params);
TextView cat = new TextView(mycontext);
TableRow.LayoutParams RowParams=
new TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(85));
RowParams.weight = 1;
cat.setLayoutParams(RowParams);
cat.setPadding(35, 0, 5, 0);
cat.setGravity(0x13);
cat.setSingleLine(true);
cat.setHorizontallyScrolling(true);
cat.setFocusable(true);
cat.setFocusableInTouchMode(true);
cat.setLines(1);
cat.setText(data.getCats().get(i).getName());
cat.setTextColor(color);
cat.setSelected(true);
catName.addView(cat);
catTable.addView(catName);
for(int j=0; j < 5; j++)
{
TableRow trChild = new TableRow(mycontext);
trChild.setLayoutParams(params);
trChild.setGravity(0x11);
for (int k=0; k<4; k++)
{
TextView app = new TextView(mycontext);
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(80));
margin = pixeltoDp(11);
tableRowParams.weight = 1;
app.setLayoutParams(tableRowParams);
app.setGravity(0x11);
app.setSingleLine(true);
app.setTextSize(pixeltoDp(12));
app.setTextColor(Color.BLACK);
app.setTypeface(verdana);
int pos = j+(k*5);
if((pos < mysize))
{
Drawable icon = getMyDrawable();
app.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
app.setText("some Name");
app.setTag(i+":"+pos);
app.setEllipsize(TextUtils.TruncateAt.END);
app.setOnClickListener(onMyClick);
}
trChild.addView(app);
}
catTable.addView(trChild);
}
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams
(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
margin = pixeltoDp(5);
tableRowParams.setMargins(margin,0,margin, pixeltoDp(18));
catTable.setLayoutParams(tableRowParams);
}
row.setGravity(0x03);
table.addView(row);
return table;
}
的OnClickListener如下
private View.OnClickListener onAppClick=new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String gettext = (String)v.getTag();
String[] splittext = gettext.split(":");
Log.d("TAG","Click Event is Triggered");
}
};
我現在面臨的問題是,viewpager當我在屏幕上滑動時滑動,但如果我的手指用onclicklistener從文本開始滑動頁面不滑動,也不會調用click事件。如果我點擊文本視圖,點擊事件被調用。我無法弄清楚爲什麼viewpager在不翻頁的情況下,如果滑動從文本視圖開始點擊事件。
爲了找出錯誤的原因,我在viewpager中添加了一個ontouchlistener,並發現在沒有文本視圖和觸摸事件的頁面部分發生滑動時,會調用viewpager的touch事件如果滑動從文本視圖開始,則根本不會被調用。如果我刪除了textview的onclicklistener,那麼整個viewpager會正常翻轉而不會出現任何問題。所以問題在於textview的click事件正在停止viewpager的觸摸事件。請告訴我,如果我在我的創建頁面代碼中做錯了什麼。我在這個函數中創建的視圖通過pageadapter添加到viewpager,如我在開頭提到的教程中所示。
非常感謝你的研究和發佈這個,我以爲我瘋了一會兒...... – avalancha
謝謝!我正在尋找這一整天的原因...... –
singleLine =「true」已被棄用。使用maxLine =「1」而不是 –