text 片段沟通
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 片段沟通相关的知识,希望对你有一定的参考价值。
package com.example.android.android_me.ui;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.example.android.android_me.R;
// This activity is responsible for displaying the master list of all images
// Implement the MasterListFragment callback, OnImageClickListener
public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Define the behavior for onImageSelected
public void onImageSelected(int position) {
// Create a Toast that displays the position that was clicked
Toast.makeText(this, "Position clicked = " + position, Toast.LENGTH_SHORT).show();
}
}
package com.example.android.android_me.ui;
import android.content.Context;
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.AdapterView;
import android.widget.GridView;
import com.example.android.android_me.R;
import com.example.android.android_me.data.AndroidImageAssets;
// This fragment displays all of the AndroidMe images in one large list
// The list appears as a grid of images
public class MasterListFragment extends Fragment {
// Define a new interface OnImageClickListener that triggers a callback in the host activity
OnImageClickListener mCallback;
// OnImageClickListener interface, calls a method in the host activity named onImageSelected
public interface OnImageClickListener {
void onImageSelected(int position);
}
// Override onAttach to make sure that the container activity has implemented the callback
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the host activity has implemented the callback interface
// If not, it throws an exception
try {
mCallback = (OnImageClickListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnImageClickListener");
}
}
// Mandatory empty constructor
public MasterListFragment() {
}
// Inflates the GridView of all AndroidMe images
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_master_list, container, false);
// Get a reference to the GridView in the fragment_master_list xml layout file
GridView gridView = (GridView) rootView.findViewById(R.id.images_grid_view);
// Create the adapter
// This adapter takes in the context and an ArrayList of ALL the image resources to display
MasterListAdapter mAdapter = new MasterListAdapter(getContext(), AndroidImageAssets.getAll());
// Set the adapter on the GridView
gridView.setAdapter(mAdapter);
// Set a click listener on the gridView and trigger the callback onImageSelected when an item is clicked
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Trigger the callback method and pass in the position that was clicked
mCallback.onImageSelected(position);
}
});
// Return the root view
return rootView;
}
}
1. Define an interface in the fragment class that triggers a callbacl=k in the host activity
2. Ovveride the onAttach to make sure that the container acrivity has implemented the callback.
3. Set a click listener in the gridView and trigger the callback when an item is clicked
4. In the host activity implemenet the interface
5. Define the bahavior inside the callback method
以上是关于text 片段沟通的主要内容,如果未能解决你的问题,请参考以下文章