使用选项卡布局

时间:2016-05-18 19:20:20

标签: android

我是Android开发的新手,我不知道如何使用Listview从主要活动传递数据来选择特定产品并转到带有fragement tablayout的新活动...我想知道如何获取片段中的数据来自主要活动的tablayout ...谢谢

MainActivity.java   导入包......

  public class HomeActivity extends AppCompatActivity {

  String s_firstname, s_lastname, s_password, s_email, Err;
  TextView fname,lname,email,password,err,sid;
  private DrawerLayout drawerLayout;
  private Toolbar toolbar;
  private ListView lv;
  private List<String> classList;
  private FloatingActionButton fab;
  Context ctx=this;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    initNavigationDrawer();

    lv = (ListView) findViewById(R.id.listView);

    SharedPreferences ui = this.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
    String s_id = ui.getString("s_id","s_id");
    new SignupActivity(this).execute(s_id);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String c_name = (String) lv.getItemAtPosition(position);
            Intent intent = new Intent(HomeActivity.this,ClassDetailsActivity.class);
            intent.putExtra("c_name", c_name);
            startActivity(intent);
        }
    });

    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(HomeActivity.this,JoinclassActivity.class);
            startActivity(intent);
        }
    });
   }
 }

DetailsProduct.java

public class ClassDetailsActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_class_details);
    String name = getIntent().getExtras().getString("c_name");
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new  ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new EventFragment(), "Event");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
  }
}

OneFragement.java

public class EventFragment extends Fragment {
TextView tv;

public EventFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_event, container, false);

      }

 }

1 个答案:

答案 0 :(得分:0)

请点击此链接。展示了如何将值传递给viewpager片段。

Send values from ViewPager Activity to a Fragment by bundle