Skip to content

Commit 213e4ba

Browse files
author
xiaoqi
committed
添加fragment调用方法例子
1 parent 3d484e4 commit 213e4ba

File tree

11 files changed

+359
-37
lines changed

11 files changed

+359
-37
lines changed

.idea/misc.xml

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
implementation fileTree(include: ['*.jar'], dir: 'libs')
3131
implementation 'com.android.support:appcompat-v7:26+'
3232
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
33+
implementation 'com.android.support:recyclerview-v7:26.1.0'
3334
testImplementation 'junit:junit:4.12'
3435
androidTestImplementation 'com.android.support.test:runner:1.0.1'
3536
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.noober.floatmenu;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.support.v4.app.Fragment;
6+
import android.support.v7.widget.GridLayoutManager;
7+
import android.support.v7.widget.LinearLayoutManager;
8+
import android.support.v7.widget.RecyclerView;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
13+
import com.noober.floatmenu.dummy.DummyContent;
14+
import com.noober.floatmenu.dummy.DummyContent.DummyItem;
15+
16+
import java.util.List;
17+
18+
/**
19+
* A fragment representing a list of Items.
20+
* <p/>
21+
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
22+
* interface.
23+
*/
24+
public class ItemFragment extends Fragment {
25+
26+
// TODO: Customize parameter argument names
27+
private static final String ARG_COLUMN_COUNT = "column-count";
28+
// TODO: Customize parameters
29+
private int mColumnCount = 1;
30+
private OnListFragmentInteractionListener mListener;
31+
32+
/**
33+
* Mandatory empty constructor for the fragment manager to instantiate the
34+
* fragment (e.g. upon screen orientation changes).
35+
*/
36+
public ItemFragment() {
37+
}
38+
39+
// TODO: Customize parameter initialization
40+
@SuppressWarnings("unused")
41+
public static ItemFragment newInstance(int columnCount) {
42+
ItemFragment fragment = new ItemFragment();
43+
Bundle args = new Bundle();
44+
args.putInt(ARG_COLUMN_COUNT, columnCount);
45+
fragment.setArguments(args);
46+
return fragment;
47+
}
48+
49+
@Override
50+
public void onCreate(Bundle savedInstanceState) {
51+
super.onCreate(savedInstanceState);
52+
53+
if (getArguments() != null) {
54+
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
55+
}
56+
}
57+
58+
@Override
59+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
60+
Bundle savedInstanceState) {
61+
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
62+
63+
// Set the adapter
64+
if (view instanceof RecyclerView) {
65+
Context context = view.getContext();
66+
RecyclerView recyclerView = (RecyclerView) view;
67+
if (mColumnCount <= 1) {
68+
recyclerView.setLayoutManager(new LinearLayoutManager(context));
69+
} else {
70+
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
71+
}
72+
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
73+
}
74+
return view;
75+
}
76+
77+
78+
@Override
79+
public void onAttach(Context context) {
80+
super.onAttach(context);
81+
if (context instanceof OnListFragmentInteractionListener) {
82+
mListener = (OnListFragmentInteractionListener) context;
83+
} else {
84+
throw new RuntimeException(context.toString()
85+
+ " must implement OnListFragmentInteractionListener");
86+
}
87+
}
88+
89+
@Override
90+
public void onDetach() {
91+
super.onDetach();
92+
mListener = null;
93+
}
94+
95+
/**
96+
* This interface must be implemented by activities that contain this
97+
* fragment to allow an interaction in this fragment to be communicated
98+
* to the activity and potentially other fragments contained in that
99+
* activity.
100+
* <p/>
101+
* See the Android Training lesson <a href=
102+
* "http://developer.android.com/training/basics/fragments/communicating.html"
103+
* >Communicating with Other Fragments</a> for more information.
104+
*/
105+
public interface OnListFragmentInteractionListener {
106+
// TODO: Update argument type and name
107+
void onListFragmentInteraction(DummyItem item);
108+
}
109+
}

app/src/main/java/com/noober/floatmenu/MainActivity.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@
1212
import android.widget.ListView;
1313
import android.widget.Toast;
1414

15+
import com.noober.floatmenu.dummy.DummyContent;
1516
import com.noober.menu.FloatMenu;
1617

1718
import java.util.ArrayList;
1819
import java.util.List;
1920

20-
public class MainActivity extends AppCompatActivity {
21+
public class MainActivity extends AppCompatActivity implements ItemFragment.OnListFragmentInteractionListener {
2122

2223
private Point point = new Point();
2324
private ListView listView;
2425
private Button btn1;
2526
private Button btn2;
26-
27+
private Button btnFragment;
2728

2829
@Override
2930
protected void onCreate(Bundle savedInstanceState) {
@@ -32,10 +33,18 @@ protected void onCreate(Bundle savedInstanceState) {
3233
listView = findViewById(R.id.listview);
3334
btn1 = findViewById(R.id.btn_fun1);
3435
btn2 = findViewById(R.id.btn_fun2);
35-
36+
btnFragment = findViewById(R.id.btn_fragment);
3637
init1();
3738
init2();
3839
init3();
40+
btnFragment.setOnClickListener(new View.OnClickListener() {
41+
@Override
42+
public void onClick(View view) {
43+
findViewById(R.id.ll_content).setVisibility(View.GONE);
44+
findViewById(R.id.fl_content).setVisibility(View.VISIBLE);
45+
getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, ItemFragment.newInstance(0)).commitAllowingStateLoss();
46+
}
47+
});
3948
}
4049

4150
private void init1(){
@@ -105,4 +114,11 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
105114
}
106115
return super.dispatchTouchEvent(ev);
107116
}
117+
118+
@Override
119+
public void onListFragmentInteraction(DummyContent.DummyItem item) {
120+
FloatMenu floatMenu = new FloatMenu(MainActivity.this);
121+
floatMenu.items("菜单1", "菜单2", "菜单3");
122+
floatMenu.show(point);
123+
}
108124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.noober.floatmenu;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.TextView;
8+
9+
import com.noober.floatmenu.ItemFragment.OnListFragmentInteractionListener;
10+
import com.noober.floatmenu.dummy.DummyContent.DummyItem;
11+
12+
import java.util.List;
13+
14+
/**
15+
* {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
16+
* specified {@link OnListFragmentInteractionListener}.
17+
* TODO: Replace the implementation with code for your data type.
18+
*/
19+
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
20+
21+
private final List<DummyItem> mValues;
22+
private final OnListFragmentInteractionListener mListener;
23+
24+
public MyItemRecyclerViewAdapter(List<DummyItem> items, OnListFragmentInteractionListener listener) {
25+
mValues = items;
26+
mListener = listener;
27+
}
28+
29+
@Override
30+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
31+
View view = LayoutInflater.from(parent.getContext())
32+
.inflate(R.layout.fragment_item, parent, false);
33+
return new ViewHolder(view);
34+
}
35+
36+
@Override
37+
public void onBindViewHolder(final ViewHolder holder, int position) {
38+
holder.mItem = mValues.get(position);
39+
holder.mIdView.setText(mValues.get(position).id);
40+
holder.mContentView.setText(mValues.get(position).content);
41+
42+
holder.mView.setOnClickListener(new View.OnClickListener() {
43+
@Override
44+
public void onClick(View v) {
45+
if (null != mListener) {
46+
// Notify the active callbacks interface (the activity, if the
47+
// fragment is attached to one) that an item has been selected.
48+
mListener.onListFragmentInteraction(holder.mItem);
49+
}
50+
}
51+
});
52+
}
53+
54+
@Override
55+
public int getItemCount() {
56+
return mValues.size();
57+
}
58+
59+
public class ViewHolder extends RecyclerView.ViewHolder {
60+
public final View mView;
61+
public final TextView mIdView;
62+
public final TextView mContentView;
63+
public DummyItem mItem;
64+
65+
public ViewHolder(View view) {
66+
super(view);
67+
mView = view;
68+
mIdView = (TextView) view.findViewById(R.id.item_number);
69+
mContentView = (TextView) view.findViewById(R.id.content);
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return super.toString() + " '" + mContentView.getText() + "'";
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.noober.floatmenu.dummy;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Helper class for providing sample content for user interfaces created by
10+
* Android template wizards.
11+
* <p>
12+
* TODO: Replace all uses of this class before publishing your app.
13+
*/
14+
public class DummyContent {
15+
16+
/**
17+
* An array of sample (dummy) items.
18+
*/
19+
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();
20+
21+
/**
22+
* A map of sample (dummy) items, by ID.
23+
*/
24+
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
25+
26+
private static final int COUNT = 25;
27+
28+
static {
29+
// Add some sample items.
30+
for (int i = 1; i <= COUNT; i++) {
31+
addItem(createDummyItem(i));
32+
}
33+
}
34+
35+
private static void addItem(DummyItem item) {
36+
ITEMS.add(item);
37+
ITEM_MAP.put(item.id, item);
38+
}
39+
40+
private static DummyItem createDummyItem(int position) {
41+
return new DummyItem(String.valueOf(position), "Item " + position, makeDetails(position));
42+
}
43+
44+
private static String makeDetails(int position) {
45+
StringBuilder builder = new StringBuilder();
46+
builder.append("Details about Item: ").append(position);
47+
for (int i = 0; i < position; i++) {
48+
builder.append("\nMore details information here.");
49+
}
50+
return builder.toString();
51+
}
52+
53+
/**
54+
* A dummy item representing a piece of content.
55+
*/
56+
public static class DummyItem {
57+
public final String id;
58+
public final String content;
59+
public final String details;
60+
61+
public DummyItem(String id, String content, String details) {
62+
this.id = id;
63+
this.content = content;
64+
this.details = details;
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return content;
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)