這是很容易通過回調來實現。只需用任何方法創建一個接口,然後在Viewpager中實現。調用將在您的View類中根據您的觸摸事件調用,並且響應將發送到Viewpager。我在MainActivity中創建了一個ImageView的示例,並調用它從另一個片段更改大小,並且它可以工作。這裏是我下面的代碼:
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
}
}
我的片段類與簡單的按鈕:
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;
}
}
我相信你不需要XML文件。如果有人需要我只是共享:
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>
畫布是如何定位在viewpager控制?它顯示爲彈出窗口還是其他方式? – ridoy
畫布和viewpager在2個獨立的視圖內。仔細查看我的佈局代碼。@ridoy –
因此,您的DrawingView攔截NonSwipeableViewPager(根據您的佈局代碼),不是嗎? – ridoy