简体   繁体   English

单击后带有列表和按钮的Android选项卡不滑动

[英]Android Tabs with Lists and Buttons not sliding after click

First problem: My tabs won't slide after I press a button on it and I have to manually tap on the tabs. 第一个问题:按下选项卡后,选项卡不会滑动,而我必须手动点击选项卡。 It slides perfect before pressing the button 在按下按钮之前,它可以完美滑动

Second Problem: Also my lists is not showing up at all on my tab. 第二个问题:同样,我的列表也没有显示在我的标签上。 The adpater is suppose to spit out a value to the listview every time i press a button with input but the list just doesnt show adpater假设每次我按下带有输入的按钮时,都会向listview吐出一个值,但列表不会显示

public class tablogger extends FragmentActivity { 

ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;

ArrayAdapter<String> adapter;
ArrayList<String> list = new ArrayList<String>();
EditText benchStat;
EditText benchreps;
String benchfinals;
ListView lv;




@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tablogger);
    TabAdapter = new TabPagerAdapter(getSupportFragmentManager());



    Tab = (ViewPager)findViewById(R.id.pager);
    Tab.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() { 
                @Override 
                public void onPageSelected(int position) {
                    actionBar = getActionBar();
                    actionBar.setSelectedNavigationItem(position);                    }
            }); 
    Tab.setAdapter(TabAdapter);
    actionBar = getActionBar();
    //Enable Tabs on Action Bar 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener(){
        @Override 
        public void onTabReselected(android.app.ActionBar.Tab tab,
                                    FragmentTransaction ft) {
            // TODO Auto-generated method stub 
        } 
        @Override 
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            Tab.setCurrentItem(tab.getPosition());
        } 
        @Override 
        public void onTabUnselected(android.app.ActionBar.Tab tab,
                                    FragmentTransaction ft) {
            // TODO Auto-generated method stub 
        }}; 
    //Add New Tab 
    actionBar.addTab(actionBar.newTab().setText("Bench").setTabListener(tabListener));
    actionBar.addTab(actionBar.newTab().setText("Squat").setTabListener(tabListener));
    actionBar.addTab(actionBar.newTab().setText("Dead").setTabListener(tabListener));



} 

public void bencircle(View view) {
    setContentView(R.layout.benchtab);
    lv.setAdapter(adapter);

    int benchmax = (int) ((100 * (Integer.parseInt(benchStat.getText().toString()))) /
            (101.3 - (2.67123 * (Integer.parseInt(benchreps.getText().toString())))));
    benchfinals = Integer.toString(benchmax);


    String input = benchfinals;
    if (input.length() > 0) {
        // add string to the adapter, not the listview 
        Toast.makeText(this, benchfinals, Toast.LENGTH_SHORT).show();
        adapter.add(input);
        adapter.notifyDataSetChanged();

        // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method 

    } 
} 


public class TabPagerAdapter extends FragmentStatePagerAdapter { 

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub 
    } 
    @Override 
    public Fragment getItem(int i) {
        switch (i) {
            case 0: 
                //Fragement for Android Tab 
                return new Bench(); 
            case 1: 
                //Fragment for Ios Tab 
                return new Squat(); 
            case 2: 
                //Fragment for Windows Tab 
                return new Dead(); 
        } 
        return null; 
    } 
    @Override 
    public int getCount() { 
        // TODO Auto-generated method stub 
        return 3; //No of Tabs 
    } 
} 


public class Bench extends Fragment /* implements View.OnClickListener*/ {
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View android = inflater.inflate(R.layout.benchtab, container, false);
        ((TextView) android.findViewById(R.id.textView)).setText("");
        adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.simple_expandable_list_item_1, list);
        lv = (ListView)android.findViewById(R.id.benchlist);
        //android.findViewById(R.id.framecircle).setOnClickListener(this); 


        benchStat = (EditText)android.findViewById(R.id.benchstat_id);
        benchreps = (EditText)android.findViewById(R.id.benchrepstat_id);

        return android;
    } 

    /*@Override 
    public void onClick(View v) { 

            setContentView(R.layout.benchtab); 
            lv.setAdapter(adapter); 

            int benchmax = (int) ((100 * (Integer.parseInt(benchStat.getText().toString()))) / 
                    (101.3 - (2.67123 * (Integer.parseInt(benchreps.getText().toString()))))); 
            benchfinals = Integer.toString(benchmax); 
            Toast.makeText(getActivity(), benchfinals, Toast.LENGTH_SHORT).show(); 
        return; 

    } 
    */ 

} 

public class Squat extends Fragment {
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ios = inflater.inflate(R.layout.squattab, container, false);
        ((TextView)ios.findViewById(R.id.textView)).setText("squatter");
        return ios;
    } 
} 

public class Dead extends Fragment {
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View windows = inflater.inflate(R.layout.deadtab, container, false);
        ((TextView)windows.findViewById(R.id.textView)).setText("deadenning");
        return windows;
    } 
} 

public void finish() { 
    // Prepare data intent 
    Intent data = new Intent();
    data.putExtra("tabbenchkey", benchfinals);
    //data.putExtra("tabsquatkey", squatfinal); 
    //data.putExtra("tabdeadkey", deadfinal); 
    //data.putExtra("tabpresskey", pressfinal); 
    //data.putExtra("tabpowerkey", powerfinal); 
    // Activity finished ok, return the data 
    setResult(RESULT_OK, data);
    super.finish(); 
} 

} }

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="50sp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/bench_id"
    android:layout_marginTop="42dp"

    android:text="Weight"
    android:layout_marginLeft="24dp"
    android:textSize="18sp"
    android:layout_marginBottom="40dp"
    />
<EditText
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:id="@+id/benchstat_id"
    android:layout_marginTop="35dp"

    android:layout_marginLeft="88dp"
    android:inputType="number"
    android:maxLength="3"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/benchrep_id"

    android:layout_toRightOf="@+id/benchstat_id"
    android:text="Reps"
    android:textSize="18sp"
    android:layout_marginTop="42dp"
    android:layout_marginLeft="16dp"
    />
<EditText
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:id="@+id/benchrepstat_id"
    android:layout_toRightOf="@+id/benchrep_id"
    android:layout_marginTop="35dp"


    android:inputType="number"
    android:maxLength="2"/>

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="24dp"
    android:layout_marginTop="22dp"
    android:id="@+id/framecircle"

    >

    <at.markushi.ui.CircleButton
        android:layout_width="72dip"
        android:layout_height="72dip"
        android:src="@drawable/ic_action_tick"
        app:cb_color="#e51c23"
        app:cb_pressed_ring_width="8dip"
        android:onClick="bencircle"
        android:id="@+id/circlebench"

        />

</FrameLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/bench_id">



<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/benchlist">


    </ListView>

</LinearLayout>

<EditText
    android:id="@+id/txtItem"
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:hint="yolo"
    />

You must use : 您必须使用:

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

to provide fragments for each tab and use fragment layout to provide your button and etc. 为每个选项卡提供片段,并使用片段布局提供您的按钮等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM