我使用多个TABS并且我的标签滑动在listview外部工作,但它无法在ListView上运行。
以下是代码:
public class DistF7tabActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener {
ListView gv,gv2;
String arg;
CallServiceDist c1;
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dist_f7tab);
tabHost=(TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1=tabHost.newTabSpec("TAB 1");
spec1.setIndicator("Form");
spec1.setContent(R.id.tab01);
tabHost.addTab(spec1);
TabHost.TabSpec spec2=tabHost.newTabSpec("TAB 2");
spec2.setIndicator("Reasons");
spec2.setContent(R.id.tab02);
tabHost.addTab(spec2);
gv = (ListView) findViewById(R.id.lv);
gv2 =(ListView) findViewById(R.id.lvT2);
gv.setOnItemClickListener(this);
gv2.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
Intent AcIntent = new Intent(DistF7tabActivity.this, DistF7ACtabActivity.class);
startActivity(AcIntent);
} catch (Exception e) {
Log.d("CATCH", e.toString());
}
}
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
float lastX=0;
switch (touchevent.getAction()) {
// when user first touches the screen to swap
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
// if left to right swipe on screen
if (lastX < currentX) {
switchTabs(false);
}
// if right to left swipe on screen
if (lastX > currentX) {
switchTabs(true);
}
break;
}
}
return false;
}
public void switchTabs(boolean direction) {
if (direction) // true = move left
{
if (tabHost.getCurrentTab() == 0)
tabHost.setCurrentTab(tabHost.getTabWidget().getTabCount() - 1);
else
tabHost.setCurrentTab(tabHost.getCurrentTab() - 1);
} else
// move right
{
if (tabHost.getCurrentTab() != (tabHost.getTabWidget()
.getTabCount() - 1))
tabHost.setCurrentTab(tabHost.getCurrentTab() + 1);
else
tabHost.setCurrentTab(0);
}
}
}