如何从片段到详细视图?

时间:2017-05-10 03:22:55

标签: android listview android-fragments detailview

我有两个片段:第一个片段1片段包含一个包含两个案例的列表视图,每个案例都有一个详细信息视图。第二个片段片段2有两个需要连接到片段1的图像。当点击图像1时,我想从片段2开始,并在片段中显示案例1的详细信息。片段代码如下。

有没有办法从片段到详细视图?谢谢。

片段1

public class fragment1 extends Fragment {


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

//meal list
int[] IMAGES={R.drawable.AA,R.drawable.BB};
String[] NAMES={"Case 1","Case 2"};
String[] DESCRIPTION={ "Plastic","Wood"  };

int[] IMAGES2={};
String[] NAMES2={};
String[] DESCRIPTION2={};
Toolbar toolbar;
ListView listView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_1, container, false);
    getActivity().setTitle("Cases");


    toolbar=(Toolbar) rootView.findViewById(R.id.toolbar);
    toolbar.setTitle("Cases");

    listView=(ListView) rootView.findViewById(R.id.listView_caselist);
    CustomAdapter customAdapter=new CustomAdapter();
    listView.setAdapter(customAdapter);


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

    TextView textView_name=(TextView) view.findViewById(R.id.textView_title);
    String casename=textView_name.getText().toString();
    Toast.makeText(getActivity().getApplicationContext(),"Select case:"+casename,Toast.LENGTH_SHORT).show();
    switch(casename){
        //********add items for each case********
        case "Case1":
            IMAGES2= new int[] {R.drawable.item_1, R.drawable.item_2};
            NAMES2=new String[]{"item_1","item_2”};
    DESCRIPTION2=new String{“50”,”20”};
            OpenDetailandPassValues("Case2",IMAGES2,NAMES2);
            break;
        case "Case2":
            IMAGES2= new int[] {R.drawable.item_2, R.drawable.item_3};
            NAMES2=new String[]{"item_2","item_3”};
    DESCRIPTION2=new String{“50”,”20”};
            OpenDetailandPassValues("Case2",IMAGES2,NAMES2);
            break;
    }
}
});

    // Inflate the layout for this fragment
    return rootView;
}


// pass values to activity
private void OpenDetailandPassValues(String name,int[] IMAGE2,String[] NAMES2,String[] DESCRIPTION2){
    Intent intent = new Intent(getActivity(),MealDetail.class);
    intent.putExtra("NAME_KEY",name);
    intent.putExtra("IMAGE",IMAGE2);
    intent.putExtra("NAME",NAMES2);
    intent.putExtra("DESP",DESCRIPTION2);
    startActivity(intent);

}

//create list
class CustomAdapter extends BaseAdapter {


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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView= getActivity().getLayoutInflater().inflate(R.layout.customlayout,null);
        ImageView imageView=(ImageView) convertView.findViewById(R.id.imageView1);
        TextView textView_name=(TextView) convertView.findViewById(R.id.textView_title);
        TextView textView_desp=(TextView) convertView.findViewById(R.id.textView_description);

        imageView.setImageResource(IMAGES[position]);
        textView_name.setText(NAMES[position]);
        textView_desp.setText(DESCRIPTION[position]);
        return convertView;
    }
}


}

片段2

public class MainPage extends Fragment implements View.OnClickListener {


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    getActivity().setTitle("Main Page");
    View v = inflater.inflate(R.layout.fragment_main_page, container, false);
    Typeface myTypeface = Typeface.createFromAsset(getActivity().getAssets(),"snap_itc.ttf");


    ImageView ivfearless = (ImageView) v.findViewById(R.id.image1);
    ivfearless.setOnClickListener(this);

    ImageView ivcaptain = (ImageView) v.findViewById(R.id.image2);
    ivcaptain.setOnClickListener(this);

    return v;
}

@Override
public void onClick(View v) {
    // implements your things

    switch (v.getId()) {
        case R.id.image1:
            fragment1 Fragment2=new fragment1 ();
            FragmentManager manager2=getActivity().getSupportFragmentManager();
            manager2.beginTransaction().replace(R.id.relative_layout_for_fragment,Fragment2,Fragment2.getTag()).commit();
            break;

        case R.id.image2:
            fragment1 Fragment3=new fragment1 ();
            FragmentManager manager3=getActivity().getSupportFragmentManager();
            manager3.beginTransaction().replace(R.id.relative_layout_for_fragment, Fragment3,Fragment3.getTag()).commit();
            break;

    }
}
}

2 个答案:

答案 0 :(得分:0)

你可以在显示片段时传递Bundle对象。像

Bundle bundle = getArguements();
String showCase = bundle.getString("showCase");
// DO as you like

然后在你的Fragment1中,获得&#34; showCase&#34;的值。使用

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
       <% @post.each_with_index do |photo,n| %>
       <li data-target='#myCarousel' data-slide-to="#{n}" class="#{'active' if n==0}"></li>
       <% end %>
    </ol>
    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <% if Post.first==nil %>
                <img src="http://hometelvietnam.com/wp-content/uploads/2016/12/a614f72d-c487-438a-a16f-514e65406ed2.jpg" >
                <div class="slider">
                <button class="btn btn-danger"><h1 style="margin:5px; shadow: 0px 0px 0px 5 black;">Become a host now</h1></button>
                </div>
            <%else%>
            <% image_tag Post.first.post_picture %>
            <%end%>
        </div>
        <% @post.drop(1).each do |photo| %>
        <div class="item">
            <%= link_to (image_tag photo.post_picture),photo %>
        </div>
        <%end%>
    </div>
</div>

答案 1 :(得分:0)

我建议使用发布订阅方法的概念,以消除依赖关系的紧密耦合。您可以结帐事件总线,这是一个Android优化的事件总线,简化了活动,碎片,线程,服务等之间的通信。减少代码,提高质量。

另一个选择是使用bundle并在创建时将它们传递给片段。甚至考虑键/值对存储和检索的共享首选项。

另一个有趣的想法是使用来自rxjava和rxandroid的obsevables。包含数据的类可以发布数据,片段可以订阅data.Consider PublishSubject。

希望这很有用:)