从ArrayAdapter启动片段

时间:2015-11-29 04:46:20

标签: android android-fragments fragment

我有4个片段,我用于我的应用程序。我的一个片段(ChatList)有一个ListView,它使用一个扩展ArrayAdapter的适配器。现在在我的ArrayAdapter类中,当单击某一行时,我想打开我的CurrentChat片段。我该怎么做?到目前为止,我看起来好像片段只能从活动和其他片段中访问。

ChatList.java

package com.example.jj.fragments;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * Created by jj on 11/23/2015.
 */
public class ChatList extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";
    ListView chatListLV;
    ChatListAdapter adapter;

    public static ChatList newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        ChatList fragment = new ChatList();
        fragment.setArguments(args);
        return fragment;
    }

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

    }

    // Inflate the fragment layout we defined above for this fragment
    // Set the associated text for the title
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chatlist, container, false);
        chatListLV = (ListView) view.findViewById(R.id.chatLV);
        adapter = new ChatListAdapter(getContext(),R.layout.single_chatlist_row);
        chatListLV.setAdapter(adapter);
        chatListLV.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                chatListLV.setSelection(adapter.getCount() - 1);
            }
        });
        fillChatList();
        return view;

    }

    public void fillChatList(){
        DBHelper db = new DBHelper(getActivity());
        db.getChatList(adapter);
        db.close();

    }
}

ChatListAdapter.java

package com.example.jj.fragments;

/**
 * Created by jj on 11/28/2015.
 */

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import io.socket.client.IO;
import io.socket.client.Socket;

public class ChatListAdapter extends ArrayAdapter<ChatListDataProvider>{

    public Socket mSocket;
    {
        try {
            mSocket = IO.socket("https://great-sarodh.c9.io/");
        }
        catch (URISyntaxException e){}
    }

    private static final String TAG = "ChatListAdapter" ;
    public List<ChatListDataProvider> chat_list = new ArrayList<ChatListDataProvider>();

    private TextView dateTV;
    private TextView messageTV;
    private TextView timeTV;
    private TextView sideTV;
    private ImageView PictureIV;
    private String gcmID;
    private String androidID;
    private String roomhash;
    private int side;
    CurrentChat CCFrag;

    int type;

    Context CTX;


    public ChatListAdapter(Context context, int resource) {

        super(context, resource);
        CTX = context;
    }

    @Override
    public void add(ChatListDataProvider object){
        chat_list.add(object);
        super.add(object);

    }

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

    @Override
    public ChatListDataProvider getItem(int position) {

        return chat_list.get(position);
    }


    @Override public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null){

            LayoutInflater inflator = (LayoutInflater) CTX.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.single_chatlist_row,parent,false);
        }

        sideTV = (TextView) convertView.findViewById(R.id.sideTV);
        messageTV = (TextView) convertView.findViewById(R.id.lastmsgTV);
        timeTV = (TextView) convertView.findViewById(R.id.timeTV);
        ChatListDataProvider provider = chat_list.get(position);
        gcmID = provider.gcmID;
        messageTV.setText(provider.lastMsg);
        timeTV.setText(provider.time);
        side = provider.side;
        if(side == 0){
            sideTV.setText("Who?:");
        }
        else if(side == 1){
            sideTV.setText("You:");
        }
        roomhash = provider.roomID;

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stuck
                Log.d(TAG, "JOINING ROOMID" + chat_list.get(position).roomID);
                mSocket.emit("CreateRoom", chat_list.get(position).roomID, chat_list.get(position).gcmID);
               //THE CODE TO OPEN CURRENTCHAT FRAGMENTS SHOULD GO HERE
            }
        });

        notifyDataSetChanged();
        return convertView;

    }


}

ChatListDataProvider.java

package com.example.jj.fragments;

import android.support.v4.app.FragmentActivity;

/**
 * Created by jj on 11/28/2015.
 */
public class ChatListDataProvider extends FragmentActivity{
    public String roomID;
    public String gcmID;
    public String lastMsg;
    public String date;
    public String time;
    public int side;

    private ChatAdapter chat;


    public ChatListDataProvider (String gcmID) {
        super();
    }


    public ChatListDataProvider (String roomID, String gcmID, String lastMsg, int side, String time, String date) {
        this.roomID = roomID;
        this.gcmID = gcmID;
        this.lastMsg = lastMsg;
        this.side = side;
        this.date = date;

    }

}

CurrentChat.java

package com.example.jj.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class CurrentChat extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";

    public static CurrentChat newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        CurrentChat fragment = new CurrentChat();
        fragment.setArguments(args);
        return fragment;
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.currentchat, container, false);
        return view;
    }

}

1 个答案:

答案 0 :(得分:1)

为了做到这一点,你可以在你的适配器中创建一个函数来传递你当前的活动上下文和容器/框架布局。

public void changeFragmentFromAdapter(Activity act , int layoutid)
{
    YourFragment fragmentToPopulate =  new YourFragment();
    FragmentManager frgManager = act.getFragmentManager();
    FragmentTransaction fgTransation = frgManager.beginTransaction();
    FgTransation.replace(layoutid, fragmentToPopulate).commit();
}

将此功能粘贴到ChatListAdapter中。 现在在你的ChatList类中将itemclicklistener添加到你的列表中

    chatListLV.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
                  //Here Add call change function
           adapter.changeFragmentFromAdapter(getActivity() , R.layout.chatList);
   }
}

我假设R.layout.chatList是MainActivity中3个片段的容器。 现在从你的片段活动中调用这个函数。