It is pretty easy to implement by using call back. Just create an Interface with whatever methods, you need and then implement in your Viewpager. Call will be invoked in your View class based on your touch events and responds will come to Viewpager. I have created an example with an ImageView in my MainActivity and called to changed its size from a different fragment and it works. Here is my code below:
public interface ICallBackForResize {
void resizeme();
void actionUp();
void actionDown();
}
MainActivity:
public class MainActivity extends AppCompatActivity implements ICallBackForResize {
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img= (ImageView) findViewById(R.id.image);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void resizeme() {
Toast.makeText(MainActivity.this,"called",Toast.LENGTH_LONG).show();
img.getLayoutParams().height += 20;
img.getLayoutParams().width += 20;
img.requestLayout();
}
@Override
public void actionUp() {
//do whatever
}
@Override
public void actionDown() {
//do whatever
}
}
My Fragment class with a simple button:
public class MyFragmentButton extends Fragment {
View view;
ICallBackForResize callBackForResize;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callBackForResize= (ICallBackForResize) getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view=inflater.inflate(R.layout.mybutton,container,false);
Button btn= (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callBackForResize.resizeme();
}
});
return view;
}
}
I believe you dont need xml file. I just shared if somebody needs:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
content_main:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="nanofaroque.com.addlistener.MainActivity">
<ImageView
android:id="@+id/image"
android:text="Hello World!"
android:layout_width="100dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"
android:layout_height="100dp" />
<fragment
android:name="nanofaroque.com.addlistener.MyFragmentButton"
android:id="@+id/headlines_fragment"
android:layout_width="100dp"
android:layout_height="100dp" />
</FrameLayout>
mybutton:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>