Unable to find code to replace detail fragment based on list item position

做~自己de王妃 提交于 2019-12-12 05:49:30

问题


For my master/detail list fragment I'm having serious issues trying to find code to replace the detail fragment based on the position of a list item. Based on Code that needs replacing (top line) I believe that the top line needs to be changed to something else. Therefore in accordance with my code, does anyone know what that line can be replaced with? All relevant help would be appreciated. The behaviour I want to achieve is just like the one in the master detail flow where if in Two-pane mode only, the detail fragment will be replaced with a fragment for whichever list item was selected. If in single pane mode the selected list item will open an activity.

Code that needs replacing (top line)

FragmentLineChooserList newFragment = new FragmentLineChooserList();
                FragmentTransaction transaction = FragmentWCLine.this.getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.detail_container, newFragment);
                transaction.commit();

FragmentWCLine.java

public class FragmentWCLine extends android.support.v4.app.Fragment {

    public final static String EXTRA_MESSAGE = "Station_key";

    private class WC {
        private CharSequence station;
        private CharSequence zone;
        private Class<? extends Activity> activityClass;
        private Class<? extends android.support.v4.app.Fragment> fragmentClass;

        public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
            this.fragmentClass = fragmentClass;
            this.activityClass = activityClass;
            this.station = getResources().getString(stationResId);
            this.zone = getResources().getString(zoneResId);
        }

        @Override
        public String toString() { return station.toString(); }
        public String getzone(){ return zone.toString(); }
    }

    private static WC[] mWC;

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_wc_line, container, false);

        // Instantiate the list of stations.
        mWC = new WC[]{
                new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
                new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
        };

        final ListView listView = (ListView)v.findViewById(R.id.list_wc);
        listView.setAdapter(new MyAdapter(getActivity(), mWC));
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mTwoPane){
                    startActivity(new Intent(getActivity(), mWC[position].fragmentClass));

                    FragmentLineChooserList newFragment = new FragmentLineChooserList();
                    FragmentTransaction transaction = FragmentWCLine.this.getActivity().getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.detail_container, newFragment);
                    transaction.commit();

                    setItemNormal();
                    View rowView = view;
                    setItemSelected(rowView);
                }
                else{
                    Intent intent = new Intent(this, mWC[position].activityClass);

                    String station = mWC[position].station;
                    intent.putExtra(EXTRA_MESSAGE, station);

                    startActivity(intent);
                }
            }

            public void setItemSelected(View view){
                View rowView = view;
                view.setBackgroundColor(Color.parseColor("#66CCCC"));

                TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
                tv0.setTextColor(Color.parseColor("#000099"));

                TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
                tv1.setTextColor(Color.parseColor("#000099"));
            }

            public void setItemNormal()
            {
                for (int i=0; i< listView.getChildCount(); i++) {
                    View v = listView.getChildAt(i);
                    v.setBackgroundColor(Color.TRANSPARENT);

                    TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
                    tv0.setTextColor(Color.WHITE);

                    TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
                    tv1.setTextColor(Color.parseColor("#B5B5B5"));
                }
            }
        });

        return v;
    }

    static class MyAdapter extends BaseAdapter {

        static class ViewHolder {
            TextView station;
            TextView zone;
        }

        LayoutInflater inflater;
        WC[] mWC;

        public MyAdapter(Context contexts, WC[] samples) {
            this.mWC = samples;
            inflater = LayoutInflater.from(contexts);
        }

        @Override
        public int getCount() {
            return mWC.length;
        }

        @Override
        public Object getItem(int position) {
            return mWC[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        /**set selected position**/
        private int selectPosition = -1;
        public void setSelectPosition(int position){
            if(position!=selectPosition){
                selectPosition = position;
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_item_dualline, null);
                viewHolder = new ViewHolder();

                viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
                viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.station.setText(mWC[position].station);
            viewHolder.zone.setText(mWC[position].getzone());

            //change item color
            if(position==selectPosition){
                //change item background
                convertView.setBackgroundColor(Color.parseColor("#000099"));
                //change text color
                viewHolder.station.setTextColor(Color.parseColor("#000099"));
            }else {

            }

            return convertView;
        }
    }
}

WCBankActivity

public class WCBankActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "Station_key";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_wc_bank);

        if (savedInstanceState == null) {
            // Get the message from the intent
            Intent intent = getIntent();
            // Notice to specify the sender Activity for the message
            String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);

            FragmentWCBank newFragment = new FragmentWCBank();
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction()‌​
            transaction.replace(R.id.detail_container, newFragment);
            transaction.commit();
        }
    }
}

FragmentWCBank

public class FragmentWCBank extends android.support.v4.app.Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);

        return v;
    }

}

Errors


回答1:


I think I understand better now. What you really want is...to start another activity and immediately launch the expected fragment, this would be clearer for others. For showing the correct fragment in another activity, you should do it in that activity. There is a good Google webpage @ Starting Another Activity. Below are code samples from that webpage, using your posted code.

Code samples:

WCBankActivity.java:

// Define this for Intent, mainly keep the key consistent between 2 activities
public final String EXTRA_MESSAGE = "Station_key";

FragmentWCLine.java:

if (mTwoPane) {
   ...
}
else {
   Intent intent = new Intent(this, mWC[position].activityClass);

   String station = mWC[position].station;
   intent.putExtra(WCBankActivity.EXTRA_MESSAGE, station);

   startActivity(intent);
}

In the other activity, WCWATActivity.java, if I understand right:

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

   if (savedInstanceState == null) {
       // Get the message from the intent
       Intent intent = getIntent();
       // Notice to specify the sender Activity for the message
       String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
       ...
       FragmentLineChooserList newFragment = new FragmentLineChooserList();
       FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
       transaction.replace(R.id.detail_container, newFragment);
       transaction.commit();
   }
}

Notes:

  1. Notice the key string EXTRA_MESSAGE from one activity to another. Define this constant final in WCBankActivity class.
  2. In onCreate(), get the station as a message, and process it.
  3. This is a technique to pass data from one Activity to another, Intent.putExtra() and getStringExtra().
  4. getActivity().getSupportFragmentManager() is used instead of FragmentWCLine.getActivity().



回答2:


For now, I see that variable mTwoPane never gets changed. Regarding your question, code FragmentTransaction transaction = FragmentWCLine and code related to it is fine to me.

Your top code of FragmentTransaction transaction = FragmentMainList. This is probably not correct, need code:

FragmentTransaction transaction = FragmentWCLine

Note:

  • FragmentWCLine is correct one, not FragmentMainList.

Suggested code, to be cleaner:

FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();

Note: When in a Fragment, getActivity() should be valid. This way you don't need to specify which fragment class your code resides.



来源:https://stackoverflow.com/questions/30683131/unable-to-find-code-to-replace-detail-fragment-based-on-list-item-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!