显示另一个片段的列表视图

时间:2016-02-05 16:10:51

标签: android listview android-fragments android-viewpager slidingpanelayout

我正在使用带有滑动面板的视图寻呼机,所以当我的面板展开时,它会创建用户请求并实例化视图持有者以在列表视图中显示它们,问题是它们可以在任何他们想要的地方实例化,我怎么知道它应该被实例化的片段。

这是我的代码

@Override
public void onPanelAnchored(View panel) {
    final View cView = panel;
    EndpointInterface Service = ServiceAuthGenerator.createService(EndpointInterface.class);
    currentID = sharedpreferences.getInt("CURRENTID", 0);
    Call<List<Ride>> call = Service.getPassengers(currentRide);
    call.enqueue(new Callback<List<Ride>>() {

        @Override
        public void onResponse(Response<List<Ride>> response, Retrofit retrofit) {
            if (response.isSuccess() && !response.body().isEmpty()) {
                dialogx.dismiss();
                ArrayList<String> myUsersName = new ArrayList<>();
                ArrayList<String> myUsersLastName = new ArrayList<>();
                ArrayList<String> myUsersMapDirection = new ArrayList<>();
                ArrayList<Integer> myUsersID = new ArrayList<>();
                ArrayList<Boolean> myUsersRole = new ArrayList<>();

                for (int i = 0; i < response.body().size(); i++) {
                    myUsersRole.add(response.body().get(i).getRole());
                    myUsersName.add(response.body().get(i).getUser().getFirst_name());
                    myUsersLastName.add(response.body().get(i).getUser().getLast_name());
                    myUsersMapDirection.add(getAdress(new LatLng(response.body().get(i).getOrigin_lat(), response.body().get(i).getOrigin_lng())));
                    myUsersID.add(response.body().get(i).getId());
                    currentName = myUsersName.get(i) + " " + myUsersLastName.get(i);
                    mMap.addMarker(new MarkerOptions().snippet(getAdress(new LatLng(response.body().get(Integer.valueOf(i)).getOrigin_lat(), response.body().get(Integer.valueOf(i)).getOrigin_lng()))).position(new LatLng(response.body().get(Integer.valueOf(i)).getOrigin_lat(), response.body().get(Integer.valueOf(i)).getOrigin_lng())).title(response.body().get(Integer.valueOf(i)).getUser().getFirst_name()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
                }

                ListAdapter userAdapter = new CustomAdapterRequest(MainMenu.this, myUsersName, myUsersLastName, myUsersMapDirection, myUsersID, myUsersRole, currentRide);
                ListView userListView = (ListView) cView.findViewById(R.id.listViewUserRequest);
                userListView.setAdapter(userAdapter);

            }

        }


        @Override
        public void onFailure(Throwable t) {
            Toast.makeText(getApplicationContext(), "no", Toast.LENGTH_SHORT).show();
        }
    });
}    

此外,这是我的适配器代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    myViewHolder holder = null;

    if (row == null) {
        LayoutInflater customInflater = (LayoutInflater) contexto.getSystemService(contexto.LAYOUT_INFLATER_SERVICE);

        row = customInflater.inflate(R.layout.custom_row_request, parent, false);
        holder = new myViewHolder(row);
        row.setTag(holder);
    } else {
        holder = (myViewHolder) row.getTag();
    }

    String singleNameItem = itemName.get(position);
    String singleLastNameItem = itemLastName.get(position);
    String singleDir = itemDirection.get(position);
    Integer singleID = itemIDs.get(position);
    Boolean singleRole = itemRoles.get(position);

    holder.tv_name.setText(singleNameItem + " " + singleLastNameItem);
    holder.tv_Direction.setText(singleDir);
    holder.im_profilepic.setImageResource(R.mipmap.profile_photo3);

    return row;
} 

我的持有人类

class myViewHolder {
    TextView tv_name;
    TextView tv_Direction;
    ImageView im_profilepic;

    myViewHolder(View v) {
        tv_name = (TextView) v.findViewById(R.id.nameText);
        tv_Direction = (TextView) v.findViewById(R.id.originText);
        im_profilepic = (ImageView) v.findViewById(R.id.ivImage);
    }

}

这是片段类

public class fragment1 extends Fragment {

    public fragment1() {
    }

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

        if (container == null) {
            return null;
        }
        return (CardView) inflater.inflate(R.layout.layout1, container, false);
    }

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

    public void setTextDestination(String origin, String Destination, Long Date, String estimatedTime, boolean singleRole) {
        TextView tv_Destination = (TextView) getView().findViewById(R.id.TextDestination);
        TextView tv_origin = (TextView) getView().findViewById(R.id.TextOrigin);
        TextView tv_Date = (TextView) getView().findViewById(R.id.textDatePager);
        TextView tv_EstimatedTiem = (TextView) getView().findViewById(R.id.estimatedTimeRoute);
        ImageView iv_roleType = (ImageView) getView().findViewById(R.id.ImgView_roleTypeLayout1);
        tv_Destination.setText(Destination);
        tv_origin.setText(origin);
        iv_roleType.setImageResource(singleRole ? R.mipmap.steerorange3 : R.mipmap.handorange3);
        tv_EstimatedTiem.setText(estimatedTime);

        java.util.Date date = new Date(Date * 1000L);
        DateFormat format = new SimpleDateFormat("dd-MM-yyyy    hh:mm a");
        format.setTimeZone(TimeZone.getDefault());
        String formatted = format.format(date);
        tv_Date.setText(formatted);
    }
}

我创建了一个fragment1列表,它是mu片段类并将其添加到列表中,然后根据列表中有多少项是我得到的实例数,我的设置文本函数正常工作但我不知道知道如何使用列表视图执行此操作! 谢谢! :d

1 个答案:

答案 0 :(得分:0)

将添加了列表视图的方法移动到实例化的片段。