如何使用相应的详细信息片段显示ListFragments

时间:2014-11-17 11:28:48

标签: android

![在此处输入图像描述] [1]任何人都可以告诉我如何使用相应的详细片段实现ListFragment。 我想在列表项的单击时在平板电脑的一侧显示列表视图,它应该在同一活动中显示相应的xml布局。

1 个答案:

答案 0 :(得分:0)

最后我解决了我的问题

 public class TestPlan extends ActionBarActivity implements OnClickListener {


public ViewPager pager = null;
String[] mValues = new String[] { "Current Date", "Week's Task", "Calender", "Time Table", "Create Event" };
Integer[] mListImage = {
      R.drawable.calendar_date,
      R.drawable.weeks_task,
      R.drawable.calendar,
      R.drawable.timetable,
      R.drawable.create_event,
};

/**
 * 
 */ 
public TestPlan() {
    // TODO Auto-generated constructor stub
} 

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

    setContentView(R.layout.activity_test_plan);


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

    PlanListAdapter adapter = new PlanListAdapter(TestPlan.this, mValues, mListImage);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        private PagerAdapter mPagerAdapter;

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                //Toast.makeText(TestPlan.this, "You Clicked at " +mValues[+ position], Toast.LENGTH_SHORT).show();

            List<Fragment> fragments = new ArrayList<Fragment>();
            switch(position) {
            case 0:
                TestPlanFragment nf1 = new TestPlanFragment();
                nf1.setLayout(R.layout.activity_current_date);
                fragments.add(nf1);
                break;
            case 1:
                TestPlanFragment nf2 = new TestPlanFragment();
                nf2.setLayout(R.layout.activity_calender);
                fragments.add(nf2);
                break;
            case 2:
                TestPlanFragment nf3 = new TestPlanFragment();
                nf3.setLayout(R.layout.activity_current_date);
                fragments.add(nf3);
                break; 
            case 3:
                TestPlanFragment nf4 = new TestPlanFragment();
                nf4.setLayout(R.layout.activity_calender);
                fragments.add(nf4);
                break;
            case 4:
                TestPlanFragment nf5 = new TestPlanFragment();
                nf5.setLayout(R.layout.activity_current_date);
                fragments.add(nf5);
                break;
            }
            this.mPagerAdapter = new PagerAdapter(getSupportFragmentManager(),
                    fragments);
            pager = (ViewPager) findViewById(R.id.viewpager);
            pager.setAdapter(mPagerAdapter);
            pager.setOffscreenPageLimit(4);
         }
});


}


/*
 * (non-Javadoc)
 * 
 * @see android.view.View.OnClickListener#onClick(android.view.View)
 */
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

@Override
protected void onResume() {
    super.onResume();

}

}

和xml文件相同的

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >

<ListView
    android:id="@+id/lv"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="0.15" >

</ListView>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tablerow" 
    android:layout_weight="1"
    />

 </LinearLayout>

 public class TestPlanFragment extends Fragment implements OnClickListener {

private Integer layout;
public TextView date = null;
public TextView event = null;



/**
 * @return the layout
 */
public Integer getLayout() {
    return layout;
}

/**
 * @param layout the layout to set
 */
public void setLayout(Integer layout) {
    this.layout = layout;
}

public TestPlanFragment() {
    // TODO Auto-generated constructor stub
    Bundle bdl = new Bundle(1);
    this.setArguments(bdl);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // fragVal = getArguments() != null ? getArguments().getInt(HEADING) :
    // 1;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = null;
    if(this.layout != null)
    v = inflater.inflate(this.layout, container, false);
    else
        v = inflater.inflate(R.layout.activity_current_date, container, false);

    date = (TextView) v.findViewById(R.id.date);
    event = (TextView) v.findViewById(R.id.event);
    if(date != null){
        date.setOnClickListener(this);
        event.setOnClickListener(this);
    }
    return v;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == date) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.pustaka.co.in/"));
        startActivity(browserIntent);
    }
    if(v == event) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.canarys.manage.sms"));
        startActivity(browserIntent);
    }
}

}