如何在传递自定义对象的同时从片段启动片段

时间:2019-04-11 11:19:17

标签: android android-fragments

基于先前的问题并使用基于片段的方法,现在的问题是如何在传递自定义数据对象的同时从片段中启动片段

public class MainActivity extends AppCompatActivity {

    public User user = new User();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_layout, user)
                .commit();
    }
}

User.java 用户列表

public class User extends Fragment {

    ListView userList;
    private ArrayList<UserVO> users;

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

        View view =  inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
        userList = view.findViewById(R.id.userList);

        users = getUsers(); // ArrayList

        userList.setAdapter(new UserAdapter());
        userList.setOnItemClickListener((adapterView, view1, i, l) -> {
            Log.d("UserList", "onItemClick: " + i);

            // how to launch Form.java Fragment while passing users[i]
        });

        return view;
    }

    class UserAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return users.size();
        }

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

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup,false);
            ((TextView) view.findViewById(android.R.id.text1)).setText(users.get(i).givenName());
            return view;
        }
    }
}

Form.java ,基本上是为了更新目的而以表格形式显示用户数据。

public class Form extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.form, container, false);
        return view;
    }
}

Q1。在User.java中,我在注释中有一个占位符,询问如何在传递Form.java(带有字段的自定义对象)的同时实例化users[i]

Q2。请查看MainActivity.java中的方法是否有问题,我看过另外一个approach与add方法,我在这里使用replace,不确定哪一个在这里。


尝试

我添加了以下代码,但对结果不满意,它与列表重叠,我希望表单具有自己的视图并将其放置在堆栈中。 “返回”按钮应将用户带回列表。

Form form = new Form();

getFragmentManager().beginTransaction()
    .add(R.id.frame_layout, form)
    .commit();

编辑2 以供审核-基于答案。

public interface IForm { // 1
   void update(UserObject userObject);
}

public class User extends Fragments implements IForm { // 2
    private Form form = new Form();

    void update(UserObject userObject) {
         // update user list with updated object.
    }

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

    View view =  inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
    userList = view.findViewById(R.id.userList);

    users = getUsers(); // ArrayList

    form.delegate = this; // 3

    userList.setOnItemClickListener((adapterView, view1, i, l) -> {
        Log.d("UserList_archive", "onItemClick: " + i);
        form.userData = users[i]; // 4
        getFragmentManager().beginTransaction()
                .replace(R.id.frame_layout, form)
                .addToBackStack(null)
                .commit();
    });

    return view;
}

public class Form {
    public IForm delegate; // 5
    public UserObject userObject; // see 4 - to display data in the form
}

1 个答案:

答案 0 :(得分:0)

在这里,您必须使用一个CallBack / Interface来插入并播放两个片段。

首先,使用可以完成此工作的方法编写一个接口,如下所示:

public interface ItemClickListener extends Parcelable{

   void onItemClick(FormData data);
}

然后在OnAttach()的两个片段中初始化此接口:

public class UserFragment extends Fragment {

private ItemClickListener onItemClick;

 @Override
public void onAttach(Context context) {
    super.onAttach(context);
    onItemClick= (ItemClickListener) context;
}

在onItemClick中使用它来传递值:

 userList.setOnItemClickListener((adapterView, view1, i, l) -> {
        Log.d("UserList", "onItemClick: " + i);

        // how to launch Form.java Fragment while passing users[i]
        onItemClick.onItemClick(adapterView.getItemAtPosition(i);
    });
}

然后在“活动”中实现此接口。

public class MainActivity extends AppCompatActivity implements ItemClickListener{

   @override
    public void onItemClick(FormData data){
       //do your stuff here with form data or whatever your object
       //Start Form Fragment and send the data through Bundle
       //https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android 
    }
}