列表视图项单击打开特定的片段viewpager

时间:2017-08-14 10:41:00

标签: java android listview android-studio android-fragments

如何通过主活动中的listview项目点击打开第二个活动中的片段。 我不知道如何通过列表视图项单击打开特定的viewpager片段,如: listview的片段1打开滚动活动的片段1,依此类推。 如果你愿意,我可以发送滚动活动java代码。

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
ListView listView;

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

    Button btnSimpleTabs = (Button) findViewById(R.id.scrollingTabs);

    btnSimpleTabs.setOnClickListener(this);
    listView = (ListView) findViewById(R.id.listView1);
    String[] values = new String[]{
            "Fragment 1   ::",
            "Fragment 2   ::",
            "Fragment 3   ::",
            "Fragment 4   ::",
            "Fragment 5   ::",
            "and so on....",
    };
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values){

    };

    listView.setAdapter(adapter);
    //linking from 1 item to other activity stars with if options//
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // global string to class
            String selectedValue = String.valueOf(parent.getItemAtPosition(position));

            switch (selectedValue) {

                case "Fragment 1   ::": {
                    Intent myIntent = new Intent(view.getContext(), ScrollableTabsActivity.class);
                    startActivityForResult(myIntent, 0);
                    break;
                }
                case "Fragment 2   ::": {
                    Intent myIntent = new Intent(view.getContext(), ScrollableTabsActivity.class);
                    startActivityForResult(myIntent, 0);
                    break;
                }
                case "Fragment 3   ::": {
                    Intent myIntent = new Intent(view.getContext(), ScrollableTabsActivity.class);
                    startActivityForResult(myIntent, 0);
                    break;
                }
                case "Fragment 4   ::": {
                    Intent myIntent = new Intent(view.getContext(), ScrollableTabsActivity.class);
                    startActivityForResult(myIntent, 0);
                    break;
                }
                case "Fragment 5   ::": {
                    Intent myIntent = new Intent(view.getContext(), ScrollableTabsActivity.class);
                    startActivityForResult(myIntent, 0);
                    break;
                }

            }


        }
    });
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.scrollingTabs:
            startActivity(new Intent(MainActivity.this, ScrollableTabsActivity.class));
            break;

    }
}
}

2 个答案:

答案 0 :(得分:0)

我认为您希望使用 ScrollableTabsActivity 类中的查看分页器打开特定片段。 首先,您需要传递参考 (which_fragment)表示您需要移动哪个片段

case "Fragment 1   ::": {
               Intent myIntent = new 
               Intent(view.getContext(), ScrollableTabsActivity.class);
               myIntent.putExtra("which_fragment", 0);//0 is reference for //move framgnet 1
               startActivityForResult(myIntent, 0);
               break;
         }

case "Fragment 2   ::": {
               Intent myIntent = new 
               Intent(view.getContext(), ScrollableTabsActivity.class);
               myIntent.putExtra("which_fragment", 1);//1 is reference for //move framgnet 2
               startActivityForResult(myIntent, 0);
               break;
         }

如此......

ScrollableTabsActivity中的

首先设置了view pager适配器,viewpager中的一个方法是 setCurrentItem

viewpager.setCurrentItem("which_fragment);

在设置适配器之后需要调用此方法

viewpager.setCurrentItem(getIntent().getExtra().getInt("which_fragment"));

答案 1 :(得分:0)

传递startActivity意图数据,您将在SecondActivity中检查并打开特定片段

Intent intent = new Intent(MainActivity.this, ScrollableTabsActivity.class);
intent.putExtra("your_specific_key", key);
startActivity(intent);

在您的新活动中,您可以按键获取数据并通过它打开片段

String s = getIntent().getStringExtra("your_specific_key");