【问题标题】:Android. ListView. Transition to fragment in activity2 from activity1安卓。列表显示。从activity1过渡到activity2中的片段
【发布时间】:2014-09-14 09:48:59
【问题描述】:

有一个活动,它有一个列表(listview)。我需要调整点击处理程序,以便当您单击第一个项目时调用 Activity2 显示第一个片段,如果您单击第二个元素,则调用 Activity2 显示第二个片段。

我的代码Activity1:

public class Class extends Activity {
ArrayAdapter<String> adapter = null;
View emptyView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity);
    getActionBar().hide();
    ListView listView = (ListView) findViewById(R.id.ListView);
    listView.setEmptyView(
            emptyView = getLayoutInflater().inflate(R.layout.empty_view, null));
    ((ViewGroup)listView.getParent()).addView(emptyView);
    fillListView();
    listView.setAdapter(adapter);
    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // When clicked, show a toast with the TextView text
           if (((TextView) view).getText() == "Go1"){

           } else
           if (((TextView) view).getText() == "Go2"){

             /*  FragmentManager fragmentManager1 = getFragmentManager();
               FragmentTransaction fragmentTransaction1 = fragmentManager1
                       .beginTransaction();
               Intent Fragment1 = new Intent(Class.this, MainActivity.class);
               startActivity(Fragment1);
               Fr myFragment1 = new Fr();
               myFragment1.setRetainInstance(true);
               fragmentTransaction1.replace(R.id.container, myFragment1);
               fragmentTransaction1.commit();
               SearchActivity.this.finish();*/
           }
            if (((TextView) view).getText() == "Go3"){

            }
        }
    });

    EditText editFilter = (EditText) findViewById(R.id.edit_search);
    editFilter.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            adapter.getFilter().filter(s.toString());
        }
    });

}

private void fillListView() {
    List<String> catList = new ArrayList<String>();
    catList.add("Go1");
    catList.add("Go2");
    catList.add("Go3");

    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, catList);
}

我的代码Activity2:

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
    selectItem(i);

    }
}


private void selectItem(int i) {
    String str = toString().trim();
    switch(i){

        case 1:

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            Quadrat myFragment = new Quadrat();
            myFragment.setRetainInstance(true);
            fragmentTransaction.replace(R.id.container, myFragment);
            fragmentTransaction.commit();

            break;
        case 2:

            FragmentManager fragmentManager1 = getFragmentManager();
            FragmentTransaction fragmentTransaction1 = fragmentManager1
                    .beginTransaction();
            Qoob myFragment1 = new Qoob();
            myFragment1.setRetainInstance(true);
            fragmentTransaction1.replace(R.id.container, myFragment1);
            fragmentTransaction1.commit();

            break;

        default:;
    }
}

【问题讨论】:

  • 那么问题到底是什么?你有什么问题?你知道如何使用意图启动活动吗?在 onclick 处理程序中,您最好执行 if ( position == 0) {start activity2 with fragment1} else if ( position == 1 ) {start activity2 with fragment2}

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


【解决方案1】:
         Intent Fragment1 = new Intent(Class.this, MainActivity.class);
           startActivity(Fragment1);

您可以指示该活动在启动时使用哪个片段。向意图添加参数;

       int fragmentnr = position;

       Intent intent = new Intent(Class.this, MainActivity.class);
       intent.putExtra("fagmentnr", fragmentnr ); 
       startActivity(intent);

那么在MainActivity的onCreate中(MainActivity?不是Activity2?)你要先确定fragmentnr;

这些带有键的 puExtra() 的工作方式与为函数使用参数相同。

【讨论】:

  • 是的。 MainActivity 是 Activity2。但这对我不起作用。 “int fragmentnr = 位置;”在 Activity1 中?
  • 将我的代码放在 onItemClick() (其他地方)并从中删除所有其他代码。
  • 我还是新手,不太明白发生了什么。我们已经引入了一个变量 fragmentnr 然后呢?如何初始化activity2?
  • 我向您展示了如何开始活动 2。但是您还没有将我的代码放入 onItemClick 函数中。请在这里做。并进行测试。
【解决方案2】:

我就把这个留在这里 活动1:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            if (((TextView) view).getText() == "Go1"){
                Intent in = new Intent(getApplicationContext(),
                        MainActivity.class);
                in.putExtra("key", 0);
                startActivity(in);

            }
            if (((TextView) view).getText() == "Go2"){
                Intent in = new Intent(getApplicationContext(),
                        MainActivity.class);
                in.putExtra("key", 1);
                startActivity(in);

            }
            if (((TextView) view).getText() == "Go3"){
                Toast.makeText(getBaseContext(), "Go3", Toast.LENGTH_SHORT).show();
            }

        }
    });

MainActivity 或 Activity2:

if (getIntent().getIntExtra("key", 0) == 0){
        FragmentManager fragmentManager1 = getFragmentManager();
        FragmentTransaction fragmentTransaction1 = fragmentManager1
                .beginTransaction();
        Quadrat myFragment1 = new Quadrat();
        myFragment1.setRetainInstance(true);
        fragmentTransaction1.replace(R.id.container, myFragment1);
        fragmentTransaction1.commit();
    }

代码MainActivity写入方法onCreate

【讨论】:

    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多