Home » Android » Custom Adapter For Listview in Fragment

Custom Adapter For Listview in Fragment

Posted by: admin November 1, 2017 Leave a comment

Questions:

I want to make a CustomAdapter For ListView In Fragment.

I want this type of ListView with Horizontal Scrolling:

------------------------------------------
|       |  |       |  |       |  |       |
|WebView|  |WebView|  |WebView|  |WebView|
|       |  |       |  |       |  |       |
[.Title.]  [.Title.]  [.Title.]  [.Title.]
------------------------------------------

I made a Class (URLStore) with Array

static String[] URLs = {
        "http://google.com",
        "http://facebook.com"
};
static String[] Title = {
        "Google",
        "Facebook"
};

I used these codes to make Adapter:

public class WebViews extends Fragment {
    OnURLSelectedListener mListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("WebViews", "onCreate()");
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.v("WebViews", "onActivityCreated().");
        Log.v("ViewsavedInstanceState", savedInstanceState == null ? "true" : "false");
        displayListView();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.v("WebViews", "onCreateView()");
        Log.v("WebViewContainer", container == null ? "true" : "false");
        Log.v("ViewsavedInstanceState", savedInstanceState == null ? "true" : "false");
        if (container == null) {
            return null;
        }
        View view = inflater.inflate(R.layout.xemptabsview, container, false);
        return view;
    }
    public interface OnURLSelectedListener {
        public void onURLSelected(String URL);
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnURLSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnURLSelectedListener");
        }
    }
private void displayListView() {
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.viewlayout,  URLStore.URLs);
        ListView listView = (ListView) getView().findViewById(R.id.xemptablisting);
        listView.setAdapter(dataAdapter);
        listView.setTextFilterEnabled(true);
       // --------------------------
       // How I load URL to WebView        
       // What to write here ? :(
       //---------------------------
    }
}

Please help to solve this problem I don’t know how to call WebView in
Fragment and loadUrl from Fragment

Answers: